WebKit.WKWebView class is used to implement a micro web browser in iOS app or Mac OS app. It provide several method to load html url page, local html file etc. But when i use it to load and display html page in iOS version 9 and later, i find it can not display the html page. I can only see a blank screen that is whole white without the web page. My Xcode version is 11. After some investigate i finally find the method to fix it.
1. How To Fix WKWebView Load Request Return Blank Page Error.
Below is the source code to load a web page by http url. When i run it, it only display a blank page.
// WKWebView exist in WebKit package. import WebKit // Create an instance of WKWebView. var webView: WKWebView! webView = WKWebView() // Create a URL object with provided url. The url is http protocol. let url:URL? = URL(string: "http://www.google.com") // Create a URLRequest object with above url. let request:URLRequest = URLRequest(url: url!) // Load above url request, it should display the specified url page content in the WKWebView object, but in this example it only display a blank page. webView.load(request)
This is because when you load the url http://www.google.com, google will redirect the request to it’s https version as https://www.google.com. So to load https protocol web page url, you should edit the Xcode project’s Info.plist file. And add App Transport Security Settings —> Allow Arbitrary Loads item in it like below.
- Click the Xcode project’s Info.plist file to open and edit it.
- Then click the plus button at the end of Information Property List item to add one property item.
- Choose App Transport Security Settings item from the new item drop down list.
- Click the plus icon at the end of this newly added item.
- And select Allow Arbitrary Loads item from the drop down list. And select value YES for this newly added item.
- Now when you run your app, you can see the WKWebView object display the specified web page url.
- If you right click the Info.plist file, and click Open As —> Source Code menu, it will display the xml version of Info.plist file. You can find that it add below xml content in Info.plist source code.
<key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict>