I am fooling around with using Javascript to call Objective-C and I can't get this seemingly simple task to work. I'd like to launch a simple NSAlert and write an NSLog, but I cannot get these results. Here is my AppDelegate implementation:
and the html:
I get one button like I expect, but clicking it has no effect. Interestingly enough, adding a simple alert instead of my Objective-C code doesn't work either. It does however work in Safari when I run the exact same HTML code in my browser.
Why won't things work in my WebView?
Code:
-(void)awakeFromNib{
[self.webView setDrawsBackground:NO];
NSString *resourcesPath = [[NSBundle mainBundle] resourcePath];
NSString *htmlPath = [resourcesPath stringByAppendingString:@"/index.html"];
[[self.webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:htmlPath]]];
[[self.webView windowScriptObject] setValue:self forKey:@"AppDelegate"];
}
- (void)showMessage:(NSString *)message
{
NSRunAlertPanel(@"Message from JavaScript", message, nil, nil, nil);
NSLog(@"foo");
}
and the html:
Code:
<!DOCTYPE html>
<html>
<body>
<input id="message_button" type="button" value="Show Message" onClick="window.AppDelegate.showMessage_('Hello there...');" />
</body>
</html>
I get one button like I expect, but clicking it has no effect. Interestingly enough, adding a simple alert instead of my Objective-C code doesn't work either. It does however work in Safari when I run the exact same HTML code in my browser.
Why won't things work in my WebView?