Alex Rivera | Logout

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

Asked 2011-07-26T16:02:19.863
11

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.

Edit
Report

1 Answer

1

mserin, this question was directed at the Cocoa Framework for Mac Os X. You are using a UIWebView which is part of UIKit for Cocoa Touch. If you're trying to build this for iPhone / iPad you should look at this post.

If you're trying to do this fon Mac use the code we're using here but you need to include the WebKit framework like this:

#import <WebKit/WebKit.h>

And then subclass WebView, not UIWebView:

WebView *webView;

With a property like this:

@property (nonatomic, retain) IBOutlet WebView *webView;
answered 2011-08-04T11:38:50.990

Your Answer