swiftxcodenginxwkwebviewios12

iOS 12 wkwebview not working with redirects?


I have a basic webview that loads a website that is fronted by an nginx reverse proxy that is just forwarding it to another site. I am able to load it using safari, chrome firefox etc on the device and emulator (as well as computer), but when I try to load it in the wkwebview it flashes a couple times then goes to a blank white screen. Note this same app worked fine in iOS 10 - 11, but is now broke with iOS 12. Below is a simple code excerpt that shows what I'm doing:

import UIKit
import WebKit

class ViewController: UIViewController, WKUIDelegate {

var webView: WKWebView!

override func loadView() {
    let webConfiguration = WKWebViewConfiguration()
    webView = WKWebView(frame: .zero, configuration: webConfiguration)
    webView.uiDelegate = self
    view = webView
}

override func viewDidLoad() {
    super.viewDidLoad()

    let myURL = URL(string:"https://test.com")
    let myRequest = URLRequest(url: myURL!)
    webView.load(myRequest)

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}

I've attempted adding the following to my Info.plist, which also did not work:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
    <key>NSExceptionDomains</key>
    <dict>
        <key>test.com</key>
        <dict>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSIncludesSubDomains</key>
            <true/>
        </dict>

It also shows this in the logs in xcode:

[BoringSSL] nw_protocol_boringssl_get_output_frames(1301) [C1.1:2] . 
[0x7f82f8d0efc0] get output frames failed, state 8196

When I try to debug it using Safari Dev Tools it shows that it's trying to load about:blank, which is strange, because again - it works in all other browsers. On the nginx side all I'm doing is a simple proxy_pass rule and when I hit it the endpoint in the app I can see in the nginx access logs that it responds with a 200. Anyone have ANY ideas?


Solution

  • I had the same problem and I solved it this way through the WKNavigationDelegate:

    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        if navigationAction.navigationType == .linkActivated {
            guard let url = navigationAction.request.url else {return}
            webView.load(URLRequest(url: url))
        }
        decisionHandler(.allow)
    }
    

    Hope it helps