简体   繁体   English

WKWebView与UIWebView

[英]WKWebView vs UIWebView

I've an app/project with deployment target version - iOS 10. 我有一个具有部署目标版本的应用程序/项目-iOS 10。

I had used UIWebView , which is deprecated now and replaced by WKWebView . 我曾经使用过UIWebView ,现在不推荐使用,而由WKWebView代替。 So, I want to replace UIWebView with WKWebView in my project also. 因此,我也想在我的项目中用WKWebView替换UIWebView。

It force me to either use UIWebView (with iOS 10) or change deployment target to iOS 11. 它迫使我要么使用UIWebView (与iOS 10一起使用),要么将部署目标更改为iOS 11。

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(或更高版本),请使用WKWebView否则请使用UIWebView (用于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. 问题声明: Storyboard的视图控制器不支持两个版本,我的意思是,在Storyboard中,如果将View Controller的部署目标设置为iOS 11,则应用程序在iOS 10中崩溃(并且显然应该如此),并且如果将View Controller的部署目标设置为iOS 10,然后情节提要不允许我建立项目。

For iOS 10, WKWebView shows me this error: Xcode 9 GM - WKWebView NSCoding support was broken in previous versions 对于iOS 10, WKWebView向我显示此错误: Xcode 9 GM-WKWebView NSCoding支持在先前版本中已损坏

Question: How can I make storyboard (View controller) to use WKWebView for iOS 11 and UIWebView for iOS 10? 问题:如何制作情节WKWebView (视图控制器)以使用WKWebView for iOS 11和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? 故事板(视图控制器)中是否有任何配置设置或选项可让我添加两个接口插座?

You can simply create and add a WKWebView via code. 可以简单地通过代码创建和添加WKWebView

If you want a visual representation for layout purposes in your Storyboard, here is one way to do it. 如果您希望在情节提要中以视觉方式呈现布局,这是一种实现方式。

Add a standard UIView in your view controller in your Storyboard. 在情节提要的视图控制器中添加标准UIView This will act as a "holder" for your web view. 这将充当您的Web视图的“持有人”。 Connect it to an IBOutlet , then in viewDidLoad add an instance of WKWebView as a subview of that "holder" view. 将其连接到IBOutlet ,然后在viewDidLoad添加WKWebView的实例作为该“所有者”视图的子视图。

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

    }

}

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

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