开发者

How do I get a string into a format I can use with UIWebView loadRequest?

It turns out that if you load data into a UIWebView using loadHTMLString, that the webView will always return NO for canGoBack and canGoForward properties. 开发者_运维知识库Unfortunately, I have to extract fragments of text from a large file and then put that text into a webView with loadHTMLString.

[aWebView loadHTMLString:aString baseURL:nil];

In order for the canGoBack and canGoForward properties to reflect the proper values, one must use

[aWebView loadRequest:(NSURLRequest *)request]

How can I convert a string of text into a request that can be processed with loadRequest?


You could override NSURLRequest to return your string in the HTTP body. The NSURLRequest class includes 2 messages:

-(NSData*)HTTPBody 

and 

-(NSInputStream*)HTTPBodyStream

Overriding these messages to return your NSString as either NSData or NSInputStream might work.

Converting an NSString to NSData is easy

[aStr dataUsingEncoding: NSASCIIStringEncoding];

Once you have this you can convert NSData into an NSInputStream

[NSInputStream inputStreamWithData:someData];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜