iOS 12 wkwebview无法使用重定向吗? [英] iOS 12 wkwebview not working with redirects?

查看:1062
本文介绍了iOS 12 wkwebview无法使用重定向吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本的Web视图,该视图加载一个由nginx反向代理前面的网站,该代理将其转发到另一个站点.我可以使用safari,chrome firefox等在设备和仿真器(以及计算机)上加载它,但是当我尝试在wkwebview中加载它时,它会闪烁几次,然后进入白屏. 注意该同一个应用程序在iOS 10-11上运行良好,但现在在iOS 12上无法使用.下面是一个简单的代码摘录,显示了我在做什么:

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.
}
}

我尝试将以下内容添加到我的Info.plist中,但该方法也无效:

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>

它也在xcode的日志中显示了这一点:

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

当我尝试使用Safari开发工具进行调试时,它表明它正在尝试加载about:blank,这很奇怪,因为它再次适用于所有其他浏览器.在nginx端,我正在做的只是一个简单的proxy_pass规则,当我在应用程序中命中端点时,我可以在nginx访问日志中看到它以200响应.有人有任何想法吗?

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?

推荐答案

我遇到了同样的问题,我通过WKNavigationDelegate通过以下方式解决了这个问题:

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)
}

希望有帮助

这篇关于iOS 12 wkwebview无法使用重定向吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆