0

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() } }

Lucas
  • 11
  • 5
  • Are you using a `UIWebView` or a `WKWebView`? – thm Mar 17 '17 at 15:05
  • i'm using UIWebView, i edit my post to get things clear, pls help me! – Lucas Mar 17 '17 at 17:44
  • What kind of fatal error is it? Please show the code that triggers the error and information about the error. – thm Mar 17 '17 at 17:50
  • I edit the post again. The fatal erro is when i try to stringByEvaluatingJavaScript inside passNotificationToWebView. The error is: fatal erro: unexpectedly found nil while unwrapping an Optional value – Lucas Mar 17 '17 at 19:29
  • You create a new `ZhWebViewController` instance and call `passNotificationToWebView(...)` on this object. Among other things this will lead to your @IBOutlet webView being nil. You probably want to perform this call on your existing `ZhWebViewController` instance that was loaded from the storyboard. – thm Mar 17 '17 at 20:13
  • Or you could push a new instance to your navigation controller, it depends on your app structure. But you should probably use `storyboard.instantiateViewControllerWithIdentifier(...)` to create one. (Btw., this is really turning into a very different question, I'll update my answer with some info later.) – thm Mar 17 '17 at 20:32
  • Thanks man!! I will search something about what you said, but if u post a detailed answer it's gonna be nice! – Lucas Mar 17 '17 at 21:22
  • I would need to know if you use a storyboard (and how that looks like) or if you use xibs or set up everything using code. Probably not the latter, as I see an `@IBOutlet`. – thm Mar 21 '17 at 15:20

1 Answers1

0

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. These should help you:

Original answer:

It depends on whether you are using a UIWebView or a 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.

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

In apps that run in iOS 8 and later, use the WKWeb​View class instead of using UIWeb​View.
[...]
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.

Which is further emphasized by WKWebView's API Reference:

Important
Starting in iOS 8.0 and OS X 10.10, use WKWebView to add web content to your app. Do not use UIWebView or WebView.

Community
  • 1
  • 1
thm
  • 1,217
  • 10
  • 12
  • Man, ty for answering me! I edited my post to clearify things, i hope you can solve my problem. – Lucas Mar 17 '17 at 17:45