Oh Danny Boy
Oh Danny Boy

Reputation: 4887

iPhone: Wrapping text around image

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.

Upvotes: 0

Views: 1094

Answers (1)

lucius
lucius

Reputation: 8685

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

Upvotes: 1

Related Questions