WKWebView无需重新加载页面即可评估Javascript [英] WKWebView Evaluate Javascript without reloading page

查看:108
本文介绍了WKWebView无需重新加载页面即可评估Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我只能通过将JavaScript添加到Webview的配置的userContentController中并重新加载页面来弄清楚如何评估javascript:

Currently I can only figure out how to evaluate javascript by adding it to the webview's configuration's userContentController and reloading the page like so:

WKUserScript *script = [[WKUserScript alloc] initWithSource:source injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
[self.webView.configuration.userContentController addUserScript:script];
[self.webView reload];

我如何在WKWebView上执行JavaScript,类似于旧版WebView的 stringByEvaluatingJavaScriptFromString: 这样我不必重新加载页面?

How can I execute javascript on my WKWebView similarly to the old WebView's stringByEvaluatingJavaScriptFromString: so that I don't have to reload the page?

我正在尝试获得与

[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.querySelector('meta[name=viewport]').setAttribute('content', 'width=%d;', false); ", width]];


推荐答案

[webView evaluateJavaScript:javascriptString completionHandler:nil];

执行与您为UIWebView列出的功能相同的功能

performs the same function as the one you have listed for UIWebView

这篇关于WKWebView无需重新加载页面即可评估Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆