I am trying to load a webpage with WKWebView on iOS 13 with Swift. It works fine in iOS 12. The problem is the WKWebView shows a white screen on iOS 13. The same url used for both (iOS 12/iOS 13) so I am 100% sure that there is no problem in the URL. Here is my UIViewController where I load the webpage:
import UIKit
import WebKit
class WebViewController: UIViewController , WKNavigationDelegate{
var param : String!
var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://google.com")!
webView.load(URLRequest(url: url))
}
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
}
safari inspector result :
about:blank
www.google.com
One way to ensure you will always have a valid URL before making the request is by using an 'if' instead of force unwrapping.
override func viewDidLoad() {
super.viewDidLoad()
if let url = URL(string: MyData.url){
webView.load(URLRequest(url: url))
}
}
You can also add an else statement on there or use an early return to print something out if you don't go inside.
Other than doing this to check the url is valid you can debug and check the webview to see if that is nil
. If this is the case then your solution is to add your code to viewWillAppear
instead of viewDidLoad
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With