Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load local html files with images, js and css on WebView Cocoa / Mac OS

I have code to make this work on iOS. Using whatever I found here and around the Web I managed to get somewhere making the Cocoa Mac Os version, but the images do not load. CSS and Javascript doesn't seem to be loading either. The directory for the HTML is being added to the Resources group but it's being added as Folder References (blue folder) which makes Xcode respect the directory structure of the HTML App.

Here's the code I'm trying to use:

NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"index" 
                                                     ofType:@"html" 
                                                inDirectory:@"/Patient_eMMR_HTML5" ];
NSString *html = [NSString stringWithContentsOfFile:htmlPath 
                                           encoding:NSUTF8StringEncoding 
                                              error:nil];

[[webView mainFrame] loadHTMLString:html baseURL:[NSURL fileURLWithPath:
                                                   [NSString stringWithFormat:@"%@/Patient_eMMR_HTML5/", 
                                                   [[NSBundle mainBundle] bundlePath]]]];

This is the iOS version of the code from which I based the code I use above:

NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"index" 
                                                     ofType:@"html" 
                                                inDirectory:@"/Patient_eMMR_HTML5" ];

NSString *html = [NSString stringWithContentsOfFile:htmlPath 
                                           encoding:NSUTF8StringEncoding 
                                              error:nil];

[webView loadHTMLString:html 
                baseURL:[NSURL fileURLWithPath:
                         [NSString stringWithFormat:@"%@/Patient_eMMR_HTML5/", 
                          [[NSBundle mainBundle] bundlePath]]]];

Any help is greatly appreciated. Thanks.

like image 892
HotFudgeSunday Avatar asked Jul 26 '11 16:07

HotFudgeSunday


2 Answers

There's no need to muck about with base URLs if you load the HTML via an NSURLRequest rather than as a string:

NSString* filePath = [[NSBundle mainBundle] pathForResource:@"index" 
                                                     ofType:@"html"
                                                inDirectory:@"Patient_eMMR_HTML5"];
NSURL* fileURL = [NSURL fileURLWithPath:filePath];
NSURLRequest* request = [NSURLRequest requestWithURL:fileURL];
[[webView mainFrame] loadRequest:request];

Note that you should specify the directory name only in the ‑pathForResource:… method, don't prefix it with a forward slash. It is assumed that the named directory is rooted in the main bundle's directory.

Note that UIWebView also has a ‑loadRequest: method, so you could use almost the same code on iOS.

like image 129
Rob Keniger Avatar answered Sep 17 '22 12:09

Rob Keniger


This worked for me with resourceURL on Mac OSX

[webView loadHTMLString:htmlContent baseURL:[[NSBundle mainBundle] resourceURL]]
like image 26
real 19 Avatar answered Sep 17 '22 12:09

real 19