【问题标题】:How to get JSContext from WKWebView如何从 WKWebView 获取 JSContext
【发布时间】:2014-09-11 16:00:39
【问题描述】:

在UIWebView中,我可以通过以下方式获取JSContext

[webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"]

同样的方法在 WKWebView 中不起作用,当它到达这行代码时应用程序崩溃。

有没有办法在WKWebView中获取JSContext?

提前致谢。

【问题讨论】:

  • 你找到解决 JS 回调的方法了吗?

标签: uiwebview webkit ios8 javascriptcore wkwebview


【解决方案1】:

您无法获取上下文,因为布局和 javascript 是在另一个进程中处理的。

相反,将脚本添加到您的 webview 配置中,并将您的视图控制器(或其他对象)设置为脚本消息处理程序。

现在,像这样从 JavaScript 发送消息:

window.webkit.messageHandlers.interOp.postMessage(message)

您的脚本消息处理程序将收到回调:

- (void)userContentController:(WKUserContentController *)userContentController 
                            didReceiveScriptMessage:(WKScriptMessage *)message{
    NSLog(@"%@", message.body);
}

【讨论】:

  • 是的,我已经使用过这个方法,虽然绑定原生对象到 js 上下文更方便处理 js 回调。我可以通过evaluateJavaScript调用js回调
  • 确保打开错误报告。苹果可以在中间做一些魔术,并且仍然提供功能。由于该项目是开源的,因此有人可以这样做并发布拉取请求。
  • 这样做的缺点是似乎没有办法获取有关执行期间发生的 JavaScript 错误的信息。
  • @MasonG.Zhwiti 将您的 JavaScript 包装在 try/catch 块中,并将异常传递给您的本机代码。
  • 我无法将这个答案翻译成我的具体情况,有什么建议吗? stackoverflow.com/questions/58513792/…
【解决方案2】:

像这样配置你的 wkwebview 并相应地添加处理程序并以类似的模式从脚本发布消息

NSString *myScriptSource = @"alert('Hello, World!')";


WKUserScript *s = [[WKUserScript alloc] initWithSource:myScriptSource injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:YES];
WKUserContentController *c = [[WKUserContentController alloc] init];
[c addUserScript:s];
// Add a script message handler for receiving  "buttonClicked" event notifications posted from the JS document using window.webkit.messageHandlers.buttonClicked.postMessage script message
[c addScriptMessageHandler:self name:@"buttonClicked"];

WKWebViewConfiguration *conf = [[WKWebViewConfiguration alloc] init];
conf.userContentController = c;

WKWebView *webview = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:conf];
[self.view addSubview:webview];
webview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
// Do any additional setup after loading the view, typically from a nib.
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]];
[webview loadRequest:request];

使用方法名称实现脚本消息处理程序“WKScriptMessageHandler”

#pragma mark -WKScriptMessageHandler
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message          {
if ([message.name isEqualToString:@"buttonClicked"]) {
self.buttonClicked ++;
}

// JS objects are automatically mapped to ObjC objects
id messageBody = message.body;
if ([messageBody isKindOfClass:[NSDictionary class]]) {
NSString* idOfTappedButton = messageBody[@"ButtonId"];
[self updateColorOfButtonWithId:idOfTappedButton];
}
}

并像这样发布消息表单js

var button = document.getElementById("clickMeButton");
button.addEventListener("click", function() {
    var messgeToPost = {'ButtonId':'clickMeButton'};
    window.webkit.messageHandlers.buttonClicked.postMessage(messgeToPost);
},false);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-19
    • 1970-01-01
    • 2017-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多