Migrating from UIWebView to WKWebView
UIWebView => WKWebView Equivalent
UIWebViewDelegate => WKNavigationDelegate
delegate => navigationDelegate
didFailLoadWithError => didFailNavigation
webViewDidFinishLoad => didFinishNavigation
webViewDidStartLoad => didStartProvisionalNavigation
shouldStartLoadWithRequest => decidePolicyForNavigationAction
About shouldStartLoadWithRequest
you can write:
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
print("webView:\(webView) decidePolicyForNavigationAction:\(navigationAction) decisionHandler:\(decisionHandler)")
switch navigationAction.navigationType {
case .linkActivated:
if navigationAction.targetFrame == nil {
self.webView?.loadRequest(navigationAction.request)
}
if let url = navigationAction.request.url, !url.absoluteString.hasPrefix("http://www.myWebSite.com/example") {
UIApplication.shared.open(url)
print(url.absoluteString)
decisionHandler(.cancel)
return
}
default:
break
}
if let url = navigationAction.request.url {
print(url.absoluteString)
}
decisionHandler(.allow)
}
And for the didFailLoadWithError
:
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
print("webView:\(webView) didFailNavigation:\(navigation) withError:\(error)")
let testHTML = Bundle.main.path(forResource: "back-error-bottom", ofType: "jpg")
let baseUrl = URL(fileURLWithPath: testHTML!)
let htmlString = "myErrorInHTML"
self.webView.loadHTMLString(htmlString, baseURL: baseUrl)
}
Migrate from UIWebview to WKWebview
You only need to change, the UIWebView
for WKWebView
add import WebKit
and change the class name for your Outlet to WKWebView, also change the loadRequest
for load
import UIKit
import WebKit
class ViewController: UIViewController {
@IBOutlet weak var webView: WKWebView!
let urlString: String = "https://google.com"
override func viewDidLoad() {
super.viewDidLoad()
guard let url = URL(string: urlString) else { return }
let requestObj = URLRequest(url: url)
webView.load(requestObj)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Migrate UIWebView to WKWebView
Add "WebKit" framework to your class.
Please refer the below code
import UIKit
import WebKit
class ViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
let myWebView:WKWebView = WKWebView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height))
myWebView.loadRequest(NSURLRequest(URL: NSURL(string: "http://www.communionchapelefca.org/app-home")!))
self.view.addSubview(myWebView)
}
}
Migrating iOS Hybrid App from UIWebView to WKWebview
- Yes
allowFileAccessFromFileURLs
is undocumented, so it may or may not work in iOS 13.- Yes
- Yes
- Yes, and it is required with
UIWebView
s as well. What might be different is that adding aWKWebView
to a storyboard or nib was impossible or buggy, at least until very recent versions of Xcode, which may be why you have that impression. - No, the
-webview:shouldStartLoadWithRequest:navigationType:
method is aUIWebViewDelegate
method. You want the correspondingWKNavigationDelegate
method, which is-webView:decidePolicyForNavigationAction:decisionHandler:
. - Yes
- I can't answer to that, as I've never needed to do it.
- See #8
WKWebView
has been around since iOS 8, so unless you're targeting iOS 7, Apple still may reject your app once they start rejecting for use of UIWebView
. I don't think there's any way to know if that is the case other than submitting the app to find out.
If your javascript makes use of custom schemes that rely on the webview delegate to handle them correctly (i.e., myscheme://some/callback
), it can be flaky with Webkit. This is where the script message handler that you alluded to comes in. But you have to update your javascript to use window.webkit.messageHandlers.someCallback.postMessage(someParams)
instead of using a custom URL scheme.
iOS Migration from UIWebView to WKWebView for JavaScript POST
It looks like you're on the right track. I recently wrote an article on this subject, and that should explain everything you need.
First of all, if you want to send something to the native code from your javascript code, you need to use window.webkit
in your javascript code. You'll need to implement the WKScriptMessageHandler and WKNavigationDelegate in your swift code, and implement this function:
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
//This function handles the events coming from javascript.
//We can access properties through the message body, like this:
guard let response = message.body as? String else { return }
print(response)
}
More information on this is in the article.
All the sending you will be doing will go through webview.evaluateJavascript()
. Make sure the javascript function works, because webkit errors won't show up in your Xcode console. You'll have to implement a custom messageHandler for that.
Related Topics
Wkwebview Page Height Issue on iPhone X
Getting Back a Date from a String
Didreceiveremotenotification: Fetchcompletionhandler: Open from Icon VS Push Notification
Scale Image in an Uibutton to Aspectfit
How to Set the Uinavigationbar with Gradient Color
Cropping Image with Swift and Put It on Center Position
Share Extension to Open Containing App
How to Programmatically Increase Uitableview Cell's Height in Iphone
Dateformatter Returns Unexpected Date for Timezone
Swift - Apply Local CSS to Web View
How to Disable the Network in iOS Simulator
Is There a Public Way to Force Mpnowplayinginfocenter to Show Podcast Controls
iOS Convert Large Numbers to Smaller Format