Swift version: 5.6
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 Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!
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.