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:
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?