在 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 的链接。
有传言说这是一个错误,因为有一个类似于(?)的私有函数,类似于 UIWebView 中公开可用的函数,用于评估来自 obj-C 的 javascript。
我刚开始自己研究 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 演讲)
运行 JS 代码以从 base64String 加载 .pdf 的方法示例
private func setData(_ base64String: String) {
webView.evaluateJavaScript(
"externalInterfaceLoadBase64('\(base64String)', scale = 1.0)",
completionHandler: { _, error in
if let error = error {
debugPrint("Evaluating JavaScript error: \(error)")
}
}
)
}