WKWebView 与 UIWebView

WKWebView vs UIWebView(WKWebView 与 UIWebView)

本文介绍了WKWebView 与 UIWebView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个部署目标版本为 iOS 10 的应用/项目.

I've an app/project with deployment target version - iOS 10.

我曾经使用过 UIWebView,它现在已被弃用并被 WKWebView 取代.所以,我也想在我的项目中用 WKWebView 替换 UIWebView.

I had used UIWebView, which is deprecated now and replaced by WKWebView. So, I want to replace UIWebView with WKWebView in my project also.

它迫使我要么使用 UIWebView(使用 iOS 10)或将部署目标更改为 iOS 11.

It force me to either use UIWebView (with iOS 10) or change deployment target to iOS 11.

我无法更改部署目标,但作为中间件解决方案,我添加了对两者的代码(以编程方式)支持.我的意思是,如果用户的设备操作系统是 iOS 11(或更高版本),则使用 WKWebView 否则使用 UIWebView(适用于 iOS 10 或更低版本).

I cannot change deployment target but as a middleware solution, I added code (programatically) support for both. I mean, if user's device OS is iOS 11 (or above) then use WKWebView else use UIWebView (for iOS 10 or below).

问题陈述: Storyboard 的视图控制器不支持这两个版本,我的意思是,在 Storyboard 中,如果我将视图控制器的部署目标设置为 iOS 11,那么应用程序在 iOS 10 中崩溃(显然应该如此)如果我将视图控制器的部署目标设置为 iOS 10,则情节提要不允许我构建项目.

Issue statement: Storyboard's view controller does not support both versions, I mean, in storyboard, if I set View controller's deployment target to iOS 11 then app crashes in iOS 10 (and obvious it should) and if I set view controller's deployment target to iOS 10, then storyboard does not allow me to build project.

对于 iOS 10,WKWebView 向我显示此错误:Xcode 9 GM - WKWebView NSCoding 支持在以前的版本中被破坏

For iOS 10, WKWebView shows me this error: Xcode 9 GM - WKWebView NSCoding support was broken in previous versions

问题:如何让情节提要(视图控制器)使用 iOS 11 的 WKWebView 和 iOS 10 的 UIWebView?故事板(视图控制器)中是否有任何配置设置或选项可以让我添加两个接口插座?

Question: How can I make storyboard (View controller) to use WKWebView for iOS 11 and UIWebView for iOS 10? Is there any configuration setup or option in story board (view controller) that can allow me to add both interface outlets?

推荐答案

可以通过代码简单地创建和添加一个 WKWebView.

You can simply create and add a WKWebView via code.

如果您想在 Storyboard 中为布局目的提供视觉表示,这是一种方法.

If you want a visual representation for layout purposes in your Storyboard, here is one way to do it.

在 Storyboard 的视图控制器中添加一个标准的 UIView.这将充当您的 Web 视图的持有者".将其连接到 IBOutlet,然后在 viewDidLoad 中添加 WKWebView 的实例作为该持有者"视图的子视图.

Add a standard UIView in your view controller in your Storyboard. This will act as a "holder" for your web view. Connect it to an IBOutlet, then in viewDidLoad add an instance of WKWebView as a subview of that "holder" view.

class MyViewController: UIViewController, WKNavigationDelegate {

    // standard UIView, added in Storyboard
    @IBOutlet weak var webViewHolder: UIView!

    // instance of WKWebView
    let wkWebView: WKWebView = {
        let v = WKWebView()
        v.translatesAutoresizingMaskIntoConstraints = false
        return v
    }()

    override func viewDidLoad() {

        super.viewDidLoad()

        // add the WKWebView to the "holder" UIView
        webViewHolder.addSubview(wkWebView)

        // pin to all 4 edges
        wkWebView.topAnchor.constraint(equalTo: webViewHolder.topAnchor, constant: 0.0).isActive = true
        wkWebView.bottomAnchor.constraint(equalTo: webViewHolder.bottomAnchor, constant: 0.0).isActive = true
        wkWebView.leadingAnchor.constraint(equalTo: webViewHolder.leadingAnchor, constant: 0.0).isActive = true
        wkWebView.trailingAnchor.constraint(equalTo: webViewHolder.trailingAnchor, constant: 0.0).isActive = true

        // load a URL into the WKWebView
        if let url = URL(string: "https://google.com") {
            wkWebView.load(URLRequest(url: url))
        }

        // from here on out, use wkWebView just as if you had added it in your storyboard

    }

}

这篇关于WKWebView 与 UIWebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:WKWebView 与 UIWebView

基础教程推荐