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)
}
}
SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
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.