Reputation: 103
For some reason, the WKWebView in my app won't display any websites like "https://www.google.com.au". I have tried changing the "Allow Arbitrary Loads" to "YES" in info.plist but that didn't resolve the issue.
PLEASE HELP!!!!! Thanks
My Code:
import UIKit
import WebKit
class ViewController: UIViewController {
@IBOutlet weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
displayWebPage()
}
private func displayWebPage() {
let url = URL(string: "https://www.google.com.au")
let request = URLRequest(url: url!)
webView.load(request)
}
}
Screenshot of StoryBoard:
Upvotes: 5
Views: 9188
Reputation: 1
Connect your controller to WKUIDelegate
, add loadView()
method in your View Controller and initialize your web view using WKWebViewConfiguration
.
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {
@IBOutlet weak var webView: WKWebView!
var webView: WKWebView!
override func loadView() {
let myConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: myConfiguration)
webView.uiDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
displayWebPage()
}
private func displayWebPage() {
let url = URL(string: "https://www.google.com.au")
let request = URLRequest(url: url!)
webView.load(request)
}
}
Source: WK Web View
Upvotes: -1
Reputation: 2470
set webView delegates.
webView.UIDelegate = self
webView.navigationDelegate = self
and also try by adding your domain in exception domain list
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionDomains</key>
<dict>
<key>google.com.au</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
</dict>
</dict>
Upvotes: 1
Reputation: 423
Connect the WKNavigationDelegate, this will fix the webpage not loading try the below code. Any errors thrown while loading the webpage will be printed in the webViewDidFail delegate.
class ViewController: UIViewController, WKNavigationDelegate {
@IBOutlet weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
displayWebPage()
}
private func displayWebPage() {
let url = URL(string: "https://www.google.com.au")
let request = URLRequest(url: url!)
webView.navigationDelegate = self
webView.load(request)
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
print(error)
}
}
Upvotes: 3