WKWebView与UIWebView [英] WKWebView vs UIWebView

查看:116
本文介绍了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),要么将部署目标更改为iOS11.

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).

问题声明:故事板的视图控制器不支持两个版本,我的意思是,在故事板中,如果将视图控制器的部署目标设置为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向我显示此错误:

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.

如果要在情节提要中以视觉方式表示布局,这是一种实现方法.

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

在情节提要的视图控制器中添加标准的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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆