IOS: WKWebView Vs UIWebView

I have an application / project with the target deployment version - iOS 10.

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

This forces me to either use UIWebView (with iOS 10) or change the deployment target on iOS 11.

I cannot change the purpose of the deployment, but as an interim solution I added software (software) support for both. I mean, if the user device OS is iOS 11 (or higher), use WKWebView else use UIWebView (for iOS 10 or lower).

Release statement: The storyboard view controller does not support both versions, I mean, in the storyboard, if I set the goal of deploying the View controller for iOS 11, then the application will crash in iOS 10 (and obviously it should) and if I set the goal of deploying a view controller for iOS 10, then the storyboard does not allow me to create a project.

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

Question: How to make a storyboard (View controller) use WKWebView for iOS 11 and UIWebView for iOS 10? Are there any configuration settings or options in the story panel (view controller) that can allow me to add both outputs of the interface?

+1
ios ios10 uiwebview wkwebview ios11
Oct 17 '17 at 15:18
source share
1 answer

You can simply create and add WKWebView through code.

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

Add a standard UIView to your view controller in your storyboard. This will act as a β€œholder” for your web presentation. Connect it to IBOutlet , and then in the viewDidLoad add an instance of WKWebView as a subset of this 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 } } 
+1
Oct 17 '17 at 18:35
source share



All Articles