简体   繁体   中英

Migrating iOS Hybrid App from UIWebView to WKWebview

I want to migrate my iOS hybrid app from UIWebView to WKWebView as the former has been deprecated. There are a number of similar questions to this on Stack Overflow that have already been answered but these questions focused on the rather simpler topic of just displaying a web view with out addressing the loading of local files nor the two way interaction required between the Objective C wrapper and the Javascript code for a hybrid app to deliver any functionality.

So far I have established that I need to do the following

  1. Replace the import statement for UIKit with WebKit.

  2. Before creating the wkwebview it is necessary to create a configuration object and set its key allowFileAccessFromFileURLs to TRUE.

  3. After creating the wkwebview, setting its navigationDelegate and its UIDelegate to self.

  4. When loading the url of the html/ccc/js file location, specifying allowingReadAccessToURL to delete the last path component (which I think is the file://)

  5. Set the wkwebview as a sub view of the main view (I think this was not required in UIWebView)

  6. Replacing the existing communications channel from the javascrtipt code to the Objective C code which made use of "shouldStartLoadWithRequest" by creating a script message handler in the wkwebview configuration object mentioned in 2 above and then using this message handler to invoke the processing that used to be done by "shouldStartLoadWithRequest".

  7. Replacing all existing communications channels to the javascript code from the Objective C code which made use of "stringByEvaluatingJavaScriptFromString" with "evaluateJavaScript" which now requires a completion handler which can be set to nil as I am not using any callback values.

  8. Adding a solution to allow the keyboard to be displayed without user selecting an input text field. The best I can see so far is Programmatically focus on a form in a webview (WKWebView) . I am somewhat concerned that it appears to need changing every IOS release.

  9. Addressing CORS issues. I understand that WKWebView is much stricter in its implementation of loading remote files from different URLs than UIWebView was, but I am not clear whether there is also a need to whitelist the local files to be loaded as well.

If anyone knows of a check list of things that need to be changed with tips/examples with exact details, or could provide such as an answer that would be superb.

In addition I would like to continue to support pre IOS 11 users by retaining UIWebView for these users as I believe WKWebView had issues in those earlier versions. Does anyone know if this going to cause any additional problems to resolve, and if so how?

  1. Yes
  2. allowFileAccessFromFileURLs is undocumented, so it may or may not work in iOS 13.
  3. Yes
  4. Yes
  5. Yes, and it is required with UIWebView s as well. What might be different is that adding a WKWebView 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.
  6. No, the -webview:shouldStartLoadWithRequest:navigationType: method is a UIWebViewDelegate method. You want the corresponding WKNavigationDelegate method, which is -webView:decidePolicyForNavigationAction:decisionHandler: .
  7. Yes
  8. I can't answer to that, as I've never needed to do it.
  9. 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 (ie, 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM