This code is supposed to load the Apple homepage, but instead shows a blank screen.
This happens with both HTTP and HTTPS URLs.
Code:
import UIK
The correct way with auto layout and constant 10 for all device:
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {
let webView = WKWebView()
override func viewDidLoad() {
super.viewDidLoad()
setupWebView()
}
fileprivate func setupWebView() {
webView.uiDelegate = self
webView.translatesAutoresizingMaskIntoConstraints = false
DispatchQueue.main.async {
guard let url = URL(string: "http://www.apple.com") else { return }
self.webView.load(URLRequest(url: url))
}
view.addSubview(webView)
webView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 10).isActive = true
webView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 10).isActive = true
webView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -10).isActive = true
webView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: 10).isActive = true
}}
in info.plist:
NSAppTransportSecurity
NSAllowsArbitraryLoads