iOS 12 wkwebview not working with redirects?

半腔热情 提交于 2020-01-03 10:02:23

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/52685266/ios-12-wkwebview-not-working-with-redirects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!