SOLVED
I used observer in ViewController
I have a iOS webview in Swift3 and need call Js function from AppDelegate. 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.
This is the head of my 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
@IBOutlet weak var webView: UIWebView!
This is how i call JS functions, this way works!!!
self.webView.stringByEvaluatingJavaScript(from: "someFunctionJS(\(jsonString))")
This is head of my 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.
@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()
}
}