简体   繁体   English

AppDelegate中的stringByEvaluatingJavaScript如何

[英]how stringByEvaluatingJavaScript in AppDelegate

SOLVED 解决了

I used observer in ViewController 我在ViewController中使用观察器

I have a iOS webview in Swift3 and need call Js function from AppDelegate. 我在Swift3中有一个iOS Web视图,需要从AppDelegate调用Js函数。 The reason why is because i'm using push notifications, and when the user click on notification i need to pass the information from swift3 to JS. 原因是因为我使用的是推送通知,当用户单击通知时,我需要将信息从swift3传递给JS。

This is the head of my ZhWebViewController 这是我的ZhWebViewController的负责人

class ZhWebViewController: UIViewController, UIWebViewDelegate { func passNotificationToWebView(userInfo: AnyObject){ let jsonData = try! JSONSerialization.data(withJSONObject: userInfo, options:
JSONSerialization.WritingOptions.prettyPrinted) let jsonString = NSString(data: jsonData, encoding:
String.Encoding.utf8.rawValue)! as String self.webView.stringByEvaluatingJavaScript(from: "triggerNotificationClick(\\ (jsonString))") } }

This is how i declare my UIWebView 这就是我声明我的UIWebView的方式

@IBOutlet weak var webView: UIWebView!

This is how i call JS functions, this way works!!! 这就是我所谓的JS函数的方式,这种方式有效!!!

self.webView.stringByEvaluatingJavaScript(from: "someFunctionJS(\\(jsonString))")

This is head of my AppDelegate 这是我的AppDelegate的负责人

@UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? }

This is where i receive notification and here i want to call JS function , i try call a function in ZHWebView and there try to send to JS, but i get fatal error. 这是我收到通知的地方,在这里我想调用JS函数 ,我尝试在ZHWebView中调用一个函数,然后尝试发送到JS,但是出现致命错误。

@available(iOS 10, *) extension AppDelegate : UNUserNotificationCenterDelegate{ func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { let userInfo = response.notification.request.content.userInfo ZhWebViewController().passNotificationToWebView(userInfo: userInfo as AnyObject) completionHandler() } }

Update: 更新:

It seems I totally misunderstood your question. 看来我完全误解了你的问题。 The question is not how to evaluate some JavaScript but how to get from your app delegate to your view controller. 问题不是如何评估一些JavaScript,而是如何从应用程序委托传递到视图控制器。 These should help you: 这些应该可以帮助您:

Original answer: 原始答案:

It depends on whether you are using a UIWebView or a WKWebView . 这取决于您使用的是UIWebView还是WKWebView The main difference evaluating JavaScript code between UIWebView.stringByEvaluatingJavaScript() and WKWebView.evaluateJavaScript() is that the former call is synchronous and the latter is asynchronous. UIWebView.stringByEvaluatingJavaScript()WKWebView.evaluateJavaScript()之间评估JavaScript代码的主要区别在于,前者调用是同步的,后者是异步的。

Compare 相比

let webView: UIWebView = ...
let result = webView.stringByEvaluatingJavaScript(from: "your javascript goes here")

with

let webView: WKWebView = ...
webView.evaluateJavaScript("your javascript goes here") { (result: Any?, error: Error?) in
    // handle completion
}

Please note the UIWebView API Reference states 请注意UIWebView API参考状态

In apps that run in iOS 8 and later, use the WKWeb​View class instead of using UIWeb​View. 在运行于iOS 8及更高版本的应用程序中,请使用WKWebView类而不是UIWebView。
[...] [...]
Although this method [stringByEvaluatingJavaScript] is not deprecated, best practice is to use the evaluate​Java​Script(_:​completion​Handler:​) method of the WKWeb​View class instead. 尽管不建议使用此方法[stringByEvaluatingJavaScript],但最佳实践是改为使用WKWebView类的评估JavaScript(_:completionHandler::)方法。

Which is further emphasized by WKWebView's API Reference : WKWebView的API参考中进一步强调了这一点:

Important 重要
Starting in iOS 8.0 and OS X 10.10, use WKWebView to add web content to your app. 从iOS 8.0和OS X 10.10开始,使用WKWebView将Web内容添加到您的应用程序。 Do not use UIWebView or WebView. 不要使用UIWebView或WebView。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM