Iphone – How to get a string into a format I can use with UIWebView loadRequest

iphoneuiwebview

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?

Best Answer

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];
Related Topic