UIWebView:OC调用JS

来源

方法一:
stringByEvaluatingJavaScriptFromString:

self.navigationItem.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];

方法二:
JavaScriptCore

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    //更新标题,这是上面的讲过的方法
    //self.navigationItem.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];

    //获取该UIWebView的javascript上下文
    JSContext *jsContext = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];

    //这也是一种获取标题的方法。
    JSValue *value = [self.jsContext evaluateScript:@"document.title"];
    //更新标题
    self.navigationItem.title = value.toString;
}

你可能感兴趣的:(UIWebView:OC调用JS)