iOS 14: What is new for WKWebView
In this post let's go over some of the interesting changes for
WKWebView in iOS 14.
There is new
That means mainly that you don't have to worry about accidentally interacting with some website JS code and vice versa. So no naming clashes and also protections from malicious websites that may try to influence the app via JS communication.
There are two content worlds predefined and these are
page for the website environment and
defaultClient which should be the first choice when injecting JS into the page. You can create another ones to further separate your JS code.
WKContentWorld is a new parameter in methods like
This new method will likely be the most used way for running JS code in
WKWebView. Mainly for two reasons. You can pass
arguments parameter which is a
What does the "async" in the method name means? It is an indication that this method can handle JS promises which are one of the option how JS handles asynchronous code. If your JS code returns promise then the
completionHandler of this method will not be called immediately and somehow return this promise but it will wait and you will get the value. This means
fetch to download any kind of data and then access it in Swift code.
As the name implies this method will let us find text in a webpage. You can do a bit of configuration or just use defaults that should work pretty well. The result is then selected and
WKWebView automatically scrolls so the result is visible.
Create PDF from page
Since iOS 11 we can take a snapshot of currently visible part of the page with the
createPDF page which also lets us to configure the result we want.
This was just a sort of high-level overview. For more watch the Discover WKWebView enhancements session.
Note: Not all the new stuff is available in Xcode 12 Beta 1 in Swift. You can try all the new methods with Objective-C.
Uses: Xcode 12 & Swift 5.3