iOS:WKWebView与UIWebView [英] iOS: WKWebView Vs UIWebView

查看:97
本文介绍了iOS: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).

问题陈述:故事板的视图控制器不支持这两个版本,我的意思是,在故事板中,如果我设置了View控制器的部署目标到iOS 11然后应用程序在iOS 10中崩溃(显而易见它应该)如果我将视图控制器的部署目标设置为iOS 10,那么storyboard不允许我构建项目。

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 UIWebView iOS 10?故事板(视图控制器)中是否有任何配置设置或选项可以允许我添加两个接口?

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 在Storyboard中查看控制器。这将充当您的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

    }

}

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

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