PDA

View Full Version : Loading local HTML file via IBAction in WebView




gwelmarten
Feb 19, 2012, 01:06 PM
Hi
I'm trying to make a button load a local HTML file in a webView on my OSX App. My current code is:

- (IBAction)returnHome {
[webView setUIDelegate:self];
NSURL *url = [[NSBundle mainBundle] URLForResource:@"index" withExtension:@"html"];
NSString *html = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSURL *baseUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
[[webView mainFrame] loadHTMLString:html baseURL:baseUrl];
}

And this does not load the file "index.html". I've being trauling the forums, but can't see anything similar, though I know it must be quite simple. Does anybody have any ideas as to what's wrong with that code?

Sorry if this is in the wrong place - I wasn't sure where to put it.

Thanks in advance,

Sam



pmau
Feb 19, 2012, 01:13 PM
Hi
I'm trying to make a button load a local HTML file in a webView on my OSX App. My current code is:

- (IBAction)returnHome {
[webView setUIDelegate:self];
NSURL *url = [[NSBundle mainBundle] URLForResource:@"index" withExtension:@"html"];
NSString *html = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSURL *baseUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
[[webView mainFrame] loadHTMLString:html baseURL:baseUrl];
}

And this does not load the file "index.html". I've being trauling the forums, but can't see anything similar, though I know it must be quite simple. Does anybody have any ideas as to what's wrong with that code?

Sorry if this is in the wrong place - I wasn't sure where to put it.

Thanks in advance,

Sam

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

gwelmarten
Feb 19, 2012, 01:17 PM
Ah ok - that's for iOS. Does anybody know how I would change that for OSX? The documentation for webView methods on Apple looks pretty similar for both iOS and Mac OSX.

Sam

chown33
Feb 19, 2012, 02:46 PM
Ah ok - that's for iOS.
Are you sure?

The iOS class is UIWebView. That's not the same as WebView.


Does anybody know how I would change that for OSX?
Read the WebView class reference (https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/WebKit/Classes/WebView_Class/Reference/Reference.html). More specifically, read the section on the loadRequest: method.

If you can't figure it out from the class reference doc, please explain exactly which part you're having trouble with.