在 iOS 8 中使用 WKWebView,如何从本机端运行 JavaScript 函数或以其他方式从本机端与 JavaScript 通信?似乎没有类似于 UIWebView 的stringByEvaluatingJavaScriptFromString:
.
(我可以- addScriptMessageHandler:name:
在configuration.userContentController
对象上使用以允许从 JS 到本机的通信,但我正在寻找相反的方向。)
在 iOS 8 中使用 WKWebView,如何从本机端运行 JavaScript 函数或以其他方式从本机端与 JavaScript 通信?似乎没有类似于 UIWebView 的stringByEvaluatingJavaScriptFromString:
.
(我可以- addScriptMessageHandler:name:
在configuration.userContentController
对象上使用以允许从 JS 到本机的通信,但我正在寻找相反的方向。)
(我在这里提出问题后不久就为此提交了一份雷达。)
前几天刚添加了一个新方法(感谢jcesarmobile指出):
添加
-[WKWebView evaluateJavaScript:completionHandler:]
http://trac.webkit.org/changeset/169765
该方法在 iOS 8 beta 3 及更高版本中可用。这是新的方法签名:
/* @abstract Evaluates the given JavaScript string.
@param javaScriptString The JavaScript string to evaluate.
@param completionHandler A block to invoke when script evaluation completes
or fails.
@discussion The completionHandler is passed the result of the script evaluation
or an error.
*/
- (void)evaluateJavaScript:(NSString *)javaScriptString
completionHandler:(void (^)(id, NSError *))completionHandler;
文档可在此处获得:https : //developer.apple.com/documentation/webkit/wkwebview/1415017-evaluatejavascript。
该脚本被插入到将在 WKWebView 中显示的页面中。此脚本将返回页面 URL(但您可以编写另一个 JavaScript 代码)。这意味着脚本事件是在网页上生成的,但会在我们的函数中处理:
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {...}
extension WKUserScript {
enum Defined: String {
case getUrlAtDocumentStartScript = "GetUrlAtDocumentStart"
case getUrlAtDocumentEndScript = "GetUrlAtDocumentEnd"
var name: String { return rawValue }
private var injectionTime: WKUserScriptInjectionTime {
switch self {
case .getUrlAtDocumentStartScript: return .atDocumentStart
case .getUrlAtDocumentEndScript: return .atDocumentEnd
}
}
private var forMainFrameOnly: Bool {
switch self {
case .getUrlAtDocumentStartScript: return false
case .getUrlAtDocumentEndScript: return false
}
}
private var source: String {
switch self {
case .getUrlAtDocumentEndScript, .getUrlAtDocumentStartScript:
return "webkit.messageHandlers.\(name).postMessage(document.URL)"
}
}
fileprivate func create() -> WKUserScript {
return WKUserScript(source: source,
injectionTime: injectionTime,
forMainFrameOnly: forMainFrameOnly)
}
}
}
extension WKWebViewConfiguration {
func add(script: WKUserScript.Defined, scriptMessageHandler: WKScriptMessageHandler) {
userContentController.addUserScript(script.create())
userContentController.add(scriptMessageHandler, name: script.name)
}
}
初始化 WKWebView
let config = WKWebViewConfiguration()
config.add(script: .getUrlAtDocumentStartScript, scriptMessageHandler: self)
config.add(script: .getUrlAtDocumentEndScript, scriptMessageHandler: self)
webView = WKWebView(frame: UIScreen.main.bounds, configuration: config)
webView.navigationDelegate = self
view.addSubview(webView)
捕捉事件
extension ViewController: WKScriptMessageHandler {
func userContentController (_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
if let script = WKUserScript.Defined(rawValue: message.name),
let url = message.webView?.url {
switch script {
case .getUrlAtDocumentStartScript: print("start: \(url)")
case .getUrlAtDocumentEndScript: print("end: \(url)")
}
}
}
}
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
private var webView = WKWebView()
override func viewDidLoad() {
super.viewDidLoad()
let config = WKWebViewConfiguration()
config.add(script: .getUrlAtDocumentStartScript, scriptMessageHandler: self)
config.add(script: .getUrlAtDocumentEndScript, scriptMessageHandler: self)
webView = WKWebView(frame: UIScreen.main.bounds, configuration: config)
webView.navigationDelegate = self
view.addSubview(webView)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
webView.load(urlString: "http://apple.com")
}
}
extension ViewController: WKScriptMessageHandler {
func userContentController (_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
if let script = WKUserScript.Defined(rawValue: message.name),
let url = message.webView?.url {
switch script {
case .getUrlAtDocumentStartScript: print("start: \(url)")
case .getUrlAtDocumentEndScript: print("end: \(url)")
}
}
}
}
extension WKWebView {
func load(urlString: String) {
if let url = URL(string: urlString) {
load(URLRequest(url: url))
}
}
}
extension WKUserScript {
enum Defined: String {
case getUrlAtDocumentStartScript = "GetUrlAtDocumentStart"
case getUrlAtDocumentEndScript = "GetUrlAtDocumentEnd"
var name: String { return rawValue }
private var injectionTime: WKUserScriptInjectionTime {
switch self {
case .getUrlAtDocumentStartScript: return .atDocumentStart
case .getUrlAtDocumentEndScript: return .atDocumentEnd
}
}
private var forMainFrameOnly: Bool {
switch self {
case .getUrlAtDocumentStartScript: return false
case .getUrlAtDocumentEndScript: return false
}
}
private var source: String {
switch self {
case .getUrlAtDocumentEndScript, .getUrlAtDocumentStartScript:
return "webkit.messageHandlers.\(name).postMessage(document.URL)"
}
}
fileprivate func create() -> WKUserScript {
return WKUserScript(source: source,
injectionTime: injectionTime,
forMainFrameOnly: forMainFrameOnly)
}
}
}
extension WKWebViewConfiguration {
func add(script: WKUserScript.Defined, scriptMessageHandler: WKScriptMessageHandler) {
userContentController.addUserScript(script.create())
userContentController.add(scriptMessageHandler, name: script.name)
}
}
添加您的 Info.plist 传输安全设置
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
这可能不是一种理想的方法,但根据您的用例,您可以在感染用户脚本后重新加载 WKWebView:
NSString *scriptSource = @"alert('WKWebView JS Call!')";
WKUserScript *userScript = [[WKUserScript alloc] initWithSource:scriptSource
injectionTime:WKUserScriptInjectionTimeAtDocumentEnd
forMainFrameOnly:YES];
[wkWebView.configuration.userContentController addUserScript:userScript];
[wkWebView reload];
这是对我有用的东西:
在 WKWebView 上创建一个定义“runJavaScriptInMainFrame:”方法的扩展。在扩展方法中,使用 NSInvocationOperation 调用未记录的 '_runJavaScriptInMainFrame:' 方法。
extension WKWebView {
func runJavaScriptInMainFrame(#scriptString: NSString) -> Void {
let selector : Selector = "_runJavaScriptInMainFrame:"
let invocation = NSInvocationOperation(target: self, selector: selector, object: scriptString)
NSOperationQueue.mainQueue().addOperation(invocation)
}
}
要使用,请致电:
webview.runJavacriptInMainFrame:(scriptString: "some javascript code")
感谢 Larsaronen 提供 WKWebView 私有 API 的链接。
我刚刚开始自己研究 WKWebView API,所以这可能不是最好的方法,但我认为您可以使用以下代码来完成:
NSString *scriptSource = @"console.log('Hi this is in JavaScript');";
WKUserScript *userScript = [[WKUserScript alloc]
initWithSource:scriptSource
injectionTime:WKUserScriptInjectionTimeAtDocumentStart
forMainFrameOnly:YES];
[myWKController addUserScript:userScript];
(来自 WWDC'14 演讲)