Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WKWebView is not loading web page on iOS 13

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 :

  • for iOS 13 similator is about:blank
  • for real device iOS 12.4 is www.google.com
like image 362
elhoucine ayoub Avatar asked Nov 06 '22 13:11

elhoucine ayoub


1 Answers

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.

like image 114
S.Mitchell Avatar answered Nov 15 '22 12:11

S.Mitchell