這幾天想研究一下JSContext的使用,過程中用UIWebView以如下方式加載本地的html文件:
NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"template" ofType:@"html"];
NSString *html = [NSString stringWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:nil];
[_webView loadHTMLString:html baseURL:nil];
但頁面加載完成之后蚀腿,html文件中引用的js未能正常加載吩屹。
折騰后發(fā)現(xiàn)邪财,問題在于baseURL這個(gè)參數(shù)上袭灯,以下為正確的方式晌柬。
[_webView loadHTMLString:html baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] resourcePath]]];
如果是使用loadRequest的方式姥份,也是可?以正常加載的郭脂。
獲取UIWebView中的JSContext對象,方法如下:
- (void)webViewDidFinishLoad:(UIWebView*)webView {
JSContext *jsContext = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
}