简体   繁体   English

将 iOS 混合应用程序从 UIWebView 迁移到 WKWebview

[英]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.我想将我的 iOS 混合应用程序从 UIWebView 迁移到 WKWebView,因为前者已被弃用。 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.在 Stack Overflow 上有许多与此类似的问题已经得到解答,但这些问题集中在一个相当简单的主题上,即仅显示 web 视图,而没有解决本地文件的加载问题,也没有解决目标 C 之间所需的双向交互包装器和 Javascript 代码用于混合应用程序以提供任何功能。

So far I have established that I need to do the following到目前为止,我已经确定我需要执行以下操作

  1. Replace the import statement for UIKit with WebKit.将 UIKit 的导入语句替换为 WebKit。

  2. Before creating the wkwebview it is necessary to create a configuration object and set its key allowFileAccessFromFileURLs to TRUE.在创建 wkwebview 之前,需要创建一个配置 object 并将其 key allowFileAccessFromFileURLs 设置为 TRUE。

  3. After creating the wkwebview, setting its navigationDelegate and its UIDelegate to self.创建 wkwebview 后,将其 navigationDelegate 和 UIDelegate 设置为 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://)加载html/ccc/js文件位置的url时,指定allowReadAccessToURL删除最后一个路径组件(我认为是file://)

  5. Set the wkwebview as a sub view of the main view (I think this was not required in UIWebView)将 wkwebview 设置为主视图的子视图(我认为这在 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".通过在上述 2 中提到的 wkwebview 配置 object 中创建脚本消息处理程序,将现有的通信通道从 javasccrtipt 代码替换为使用“shouldStartLoadWithRequest”的目标 C 代码,然后使用此消息处理程序调用过去的处理由“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.将所有现有的通信通道替换为目标 C 代码中的 javascript 代码,该代码使用“stringByEvaluatingJavaScriptFromString”和“evaluateJavaScript”,现在需要一个完成处理程序,因为我没有使用任何回调值,所以它可以设置为 nil。

  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) .到目前为止,我能看到的最好的是以编程方式关注 webview (WKWebView) 中的表单 I am somewhat concerned that it appears to need changing every IOS release.我有点担心它似乎需要更改每个 IOS 版本。

  9. Addressing CORS issues.解决 CORS 问题。 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.我知道 WKWebView 在从不同的 URL 加载远程文件的实现方面比 UIWebView 更严格,但我不清楚是否还需要将要加载的本地文件列入白名单。

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.此外,我想通过为这些用户保留 UIWebView 来继续支持前 IOS 11 用户,因为我相信 WKWebView 在这些早期版本中存在问题。 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. allowFileAccessFromFileURLs未记录在案,因此它在 iOS 13 中可能有效,也可能无效。
  3. Yes是的
  4. Yes是的
  5. Yes, and it is required with UIWebView s as well.是的, UIWebView也需要它。 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.可能不同的是,将WKWebView添加到 storyboard 或 nib 是不可能的或有问题的,至少在 Xcode 的最新版本之前是不可能的,这可能就是您有这种印象的原因。
  6. No, the -webview:shouldStartLoadWithRequest:navigationType: method is a UIWebViewDelegate method.不, -webview:shouldStartLoadWithRequest:navigationType:方法是UIWebViewDelegate方法。 You want the corresponding WKNavigationDelegate method, which is -webView:decidePolicyForNavigationAction:decisionHandler: .您需要相应的WKNavigationDelegate方法,即-webView:decidePolicyForNavigationAction:decisionHandler:
  7. Yes是的
  8. I can't answer to that, as I've never needed to do it.我无法回答这个问题,因为我从来不需要这样做。
  9. See #8见#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 . WKWebView自 iOS 8 以来一直存在,因此,除非您的目标是 iOS 7,否则一旦 Apple 开始拒绝使用UIWebView ,Apple 仍然可能会拒绝您的应用程序。 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.如果您的 javascript 使用依赖 webview 委托来正确处理它们的自定义方案(即myscheme://some/callback ),那么它可能会在 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.这是您提到的脚本消息处理程序的来源。但是您必须更新 javascript 以使用window.webkit.messageHandlers.someCallback.postMessage(someParams)而不是使用自定义 ZE6B391A23D2C4D457023 方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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