5

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
elhoucine ayoub
  • 949
  • 2
  • 12
  • 19
  • 1
    A couple of things you could try in order to isolate the problem: 1. Use the view inspector to verify that the WKWebView is actually in the view hierarchy. 2. Inspect the web view with the safari. You will need the safari technology preview, but it will connect to the simulator's web view and let you inspect the contents of the webview. – Rudedog Oct 01 '19 at 15:53
  • @Rudedog i tried it with google home page url ("https://google.com") and the result is blank with similator ios13 and it gives the correct response with real device ios 12.4 in safari inspector – elhoucine ayoub Oct 01 '19 at 16:58
  • Kind of a shot in the dark, but would `https://www.google.com` work? Maybe it's having a hard time parsing the URL, although I don't see why it would – Ferdz Oct 01 '19 at 17:38
  • check https://stackoverflow.com/a/59391866/810466 – Tomer Even Dec 18 '19 at 12:20

2 Answers2

1

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.

S.Mitchell
  • 91
  • 1
  • 2
  • 10
  • 1
    the url is valide , as i said it's working fine on IOS 12 but it does not on IOS 13 – elhoucine ayoub Oct 01 '19 at 14:37
  • @elhoucineayoub debug your code and check if the webview is `nil` or not. I have a feeling you are trying to make the request too early in `viewDidLoad` and will have to move it elsewhere as my answer suggests. – S.Mitchell Oct 01 '19 at 14:42
  • yes i did all what you suggested , if there is something wrong with nil value it would be nil on IOS12 too – elhoucine ayoub Oct 01 '19 at 14:50
  • 1
    In that case you can use `override func webView(WKWebView, didFailProvisionalNavigation: WKNavigation!, withError: Error)` which is called when an error occurs while the web view is loading content. Print out `withError` and this should hopefully give you some more information on where to go. – S.Mitchell Oct 01 '19 at 15:40
0

I tried it also with iOS 12.2 version on simulator and gives the same problem , so I see that the simulators does not support WKWebView , on the real device it works fine

elhoucine ayoub
  • 949
  • 2
  • 12
  • 19