Ask P
Ask P

Reputation: 405

How to inject/evaluate JS in multiple pages of an iOS webview?

Seems like android's WebViewClient has methods like: onProgressChanged()

Anything for ios' wkwebview? (or any other iOS webview)

Webview's onNavigationStateChangedoesn't seem to help here.

Would like to evaluate JS on www.example.com & then onwww.example.com/about.

Upvotes: 1

Views: 479

Answers (2)

Rob Napier
Rob Napier

Reputation: 299375

See WKUserScript, which is designed specifically for this purpose. When creating the WKWebView, you'll add a WKUserContentController, which can add scripts using addUserScript.

Upvotes: 1

Robin Schmidt
Robin Schmidt

Reputation: 1203

What exactly do you mean with onNavigationStateChange? This doesn't sound familiar to me.

You should implement WKNavigationDelegate. [https://developer.apple.com/documentation/webkit/wknavigationdelegate].

If you want to load JS after loading an url, you can implement webView(_, didFinish: WKNavigation). WKNavigation offers the api to check request / url.
To load some JS before navigation finished, you can try with webView(WKWebView, decidePolicyFor: WKNavigationAction) [https://developer.apple.com/documentation/webkit/wknavigationdelegate/1455641-webview]
or webView(WKWebView, decidePolicyFor: WKNavigationResponse) [https://developer.apple.com/documentation/webkit/wknavigationdelegate/1455643-webview].

Upvotes: 2

Related Questions