1
votes

I was trying to use WKWebView in iOS 10 and came across an error related to NSCoding.

While searching for it, I came across this article and hence decided to implement it programmatically.

I added a view controller in the storyboard and I am then adding WKUIDelegate to my controller. However even after that, I am not seeing the web page show up on my screen.

My view controller acting as web view code:

import UIKit
import WebKit

class WebViewController: UIViewController, WKUIDelegate {
    var webView: WKWebView!
    var articleURL: URL?
    @IBOutlet var webViewContainer: UIView!

    override func loadView() {
        super.loadView()
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        let webConfiguration = WKWebViewConfiguration()
        let customFrame = CGRect.init(origin: CGPoint.zero, size: CGSize.init(width: 0.0, height: self.webViewContainer.frame.size.height))
        self.webView = WKWebView (frame: customFrame , configuration: webConfiguration)
        webView.translatesAutoresizingMaskIntoConstraints = false
        self.webViewContainer.addSubview(webView)
        webView.topAnchor.constraint(equalTo: webViewContainer.topAnchor).isActive = true
        webView.rightAnchor.constraint(equalTo: webViewContainer.rightAnchor).isActive = true
        webView.leftAnchor.constraint(equalTo: webViewContainer.leftAnchor).isActive = true
        webView.bottomAnchor.constraint(equalTo: webViewContainer.bottomAnchor).isActive = true
        webView.heightAnchor.constraint(equalTo: webViewContainer.heightAnchor).isActive = true
        webView.uiDelegate = self

        guard let url = articleURL else {
            return
        }
        let myRequest = URLRequest(url: url)
        webView.load(myRequest)
    }
}

I segue into this controller from my previous controller. Code for segue:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "webPage" {
        let controller = (segue.destination as? WebViewController)!
        controller.articleURL = movieReview?.articleURL
    }
}

Storyboard image:

enter image description here

I am actually navigating to the view, but it's blank. When I debug, my code does execute 'webView.load(myRequest)', but am not seeing anything on the screen.

What else am I missing?

1
can you check your delegate method execute on request complete or Fail ?Muhammad Shauket
your code looks correct, i think you need to check your delegate methods, otherwise you can load www.google.com page for testing purpose for 100% success rateMuhammad Shauket
I tried loading google and it worked fine. I then tried my url and since it has http and not https it's not loading. I guess I will have to update the url string to https. Thanks!tech_human
thats great you found solution, then ignore my answer :) i faced same issue on friday and issue was in programatically constraint. have a good dayMuhammad Shauket

1 Answers

0
votes

First of all check your webViewContainer view is connected to in your storyboard viewcontroller. if its already connected and constraint setup correctly on it. then make constraint on webview like this way.Add constraint from self.view not from self.webview , after adding to container webview is child of your parent view so it is best to add constraint from self.view to your webView. Hope so this will work.

self.view.addConstraint(NSLayoutConstraint(item: webView, attribute: .trailing, relatedBy: .equal, toItem: self.webViewContainer, attribute: .trailing, multiplier: 1, constant: 0))
self.view.addConstraint(NSLayoutConstraint(item: webView, attribute: .leading, relatedBy: .equal, toItem: self.webViewContainer, attribute: .leading, multiplier: 1, constant: 0))
self.view.addConstraint(NSLayoutConstraint(item: webView, attribute: .top, relatedBy: .equal, toItem: self.webViewContainer, attribute: .top, multiplier: 1, constant: 0))
self.view.addConstraint(NSLayoutConstraint(item: webView, attribute: .bottom, relatedBy: .equal, toItem: self.webViewContainer, attribute: .bottom, multiplier: 1, constant: 0))