Swift version: 5.10
Using evaluateJavaScript()
you can run any JavaScript in a WKWebView
and read the result in Swift. This can be any JavaScript you want, which effectively means you can dig right into a page and pull out any kind of information you want.
Here's an example to get you started:
webView.evaluateJavaScript("document.getElementById('someElement').innerText") { (result, error) in
if error == nil {
print(result)
}
}
SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!
Sponsor Hacking with Swift and reach the world's largest Swift community!
Available from iOS 8.0 – see Hacking with Swift tutorial 4
This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.
Link copied to your pasteboard.