PDA

View Full Version : Loading local HTML in UIWebView




North Bronson
Mar 17, 2009, 02:26 AM
If I have some local HTML content that I need to load in a UIWebView, it seems like there are two ways to do it:


NSString *fileString = [[NSBundle mainBundle] pathForResource: SomeLocalHTMLFile ofType: @"html"];

NSString *newHTMLString = [[NSString alloc] initWithContentsOfFile: fileString encoding: NSASCIIStringEncoding error: NULL];

NSURL *newURL = [[NSURL alloc] initFileURLWithPath: fileString];

[myWebView loadHTMLString: newHTMLString baseURL: newURL];


OR


NSString *fileString = [[NSBundle mainBundle] pathForResource: SomeLocalHTMLFile ofType: @"html"];

NSURL *newURL = [[NSURL alloc] initFileURLWithPath: fileString];

NSURLRequest *newURLRequest = [[NSURLRequest alloc] initWithURL: newURL];

[myWebView loadRequest: newURLRequest];


Is one of these ways preferred over the other? Both seem to work.

On another note, if I do go with the first way, am I choosing the right String Encoding? I'm just loading a plain HTML file.



jnic
Mar 17, 2009, 05:57 AM
Is one of these ways preferred over the other? Both seem to work.

Basically, whichever one runs faster in a profiler. Failing that, whichever you find easier to read.

On another note, if I do go with the first way, am I choosing the right String Encoding? I'm just loading a plain HTML file.

Depends what encoding you wrote the file in. Assuming you're not doing anything special (foreign alphabets etc.), ASCII should be just fine.