1

I am showing a webView to my iOS app (Swift). From the webView I need to retrieve an object (javascript). Using the console I can see that the object I want is called "window.user" but using the following method is returning no results. Is there a way ? Thank you.

if let result = self.webView.stringByEvaluatingJavaScript(from: "document.getElementById(‘window.user’)") {
         print(result)
 }
toddg
  • 2,863
  • 2
  • 18
  • 33
PoolHallJunkie
  • 1,345
  • 14
  • 33

1 Answers1

1
  1. You need to use WKWebView (not UIWebView)
  2. You need to inject JS code into your page. Which will return your Document property. When the return event is triggered, you can handle it with your Swift code.

Example

Call JavaScript function from native code in WKWebView

Community
  • 1
  • 1
Vasily Bodnarchuk
  • 24,482
  • 9
  • 132
  • 127