1

I need to return a value in Swift by executing JavaScript, but I am having trouble I have searched everywhere, but I can't find anything that works. To execute the JavaScript, I am using

webby.evaluateJavaScript("document.querySelector(\"pre\").innerText;")

(Webby is the name of the web view) The JavaScript returns a string, which I would like to access in Swift. How would I accomplish this? Thanks.

1
  • If you took the JavaScript you have in quotes: document.querySelector(\"pre\").innerText and run it in JavaScript it won't do anything. Perhaps you need a return statement before it return document.querySelector(\"pre\").innerText Commented Feb 14, 2018 at 19:06

1 Answer 1

4

To access the value you have to provide a completion handler to evaluateJavaScript, see the docs:

webby.evaluateJavaScript("<your code goes here>") { result, error in
  if let result = result {
    // your result handling goes here
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Try to return a dummy value to see if your setup works at all, e.g. "return 'foo'". If you then get foo in your handler you know that the problem is in your JS code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.