views:

76

answers:

1

From what I have gathered, wrapping text around an image is possible using a UIWebView displaying a local html file. (I have a local html file called index.html)

The following code inserted in the viewDidLoad method seems to crash the application with error. *** Terminating app due to uncaught exception 'NSInvalidArgumentException'

This example may look familiar. It seems to be the recurring example which comes up in Google searches. If anyone knows of another way, example, or reason for why it is crashing, please let me know.

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"] isDirectory:NO]]];

Please note, "webView" is an IBOutlet of a UIWebView, and I was able to load an html string into it; just not a file. Also, the name of the html file is exactly "index.html" as stated in pathForResource and ofType.

+1  A: 

This is what I do, which has more error handling and stuff:

NSError *error = nil;
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"index" 
    ofType:@"html"];
html = [NSString stringWithContentsOfFile:filePath 
    encoding:NSUTF8StringEncoding error:&error];
if (error != nil)
    NSLog (@"Error loading index.html: %@", [error localizedDescription]);

NSString *basePath = [[NSBundle mainBundle] resourcePath];
NSURL *baseURL = [NSURL fileURLWithPath:basePath];
[webView loadHTMLString:html baseURL:baseURL];
lucius
This is a much cleaner approach. For anyone who comes across this, please remember to import your html into your project. That was causing my original problem. Thanks for the help lucius.
Oh Danny Boy