WKWebView和NSURLProtocol无法正常工作 [英] WKWebView and NSURLProtocol not working

查看:824
本文介绍了WKWebView和NSURLProtocol无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用旧的UIWebView时,您可以通过实现自定义NSURLProtocol来捕获请求。我这是为了处理需要身份验证的请求。

When using the old UIWebView you could catch the requests by implementing a custom NSURLProtocol. I us this to handle requests that requires authentication.

我尝试了相同的代码,它不适用于新的WKWebView但我的协议类根本没有被调用。是否有人遇到同样的问题,或者有更好的方法使用WKWebView进行身份验证?

I tried the same code and it doesn't work with the new WKWebView but my protocol class isn't called at all. Is someone experiencing the same problem or is there a better way of doing authentication with the WKWebView?

在没有任何修改的情况下,我在decisionPolicyForNavigationResponse委托函数中得到401响应。我还尝试使用NSURLConnection连接到服务器并使用NSURLConnectionDataDelegate处理身份验证。这有效,但WKWebView没有获取存储的凭据。

Without any modifications I get a 401 response in the decidePolicyForNavigationResponse delegate function. I've also tried connection to the server with a NSURLConnection and handling the authentication with a NSURLConnectionDataDelegate. That works but the stored credentials isn't picked up by the WKWebView.

推荐答案

iOS 11和macOS 10.13的更新答案



从iOS 11开始,可以创建 WKURLSchemeHandler 的子类,并在 WKWebView中注册它配置: - [WKWebViewConfiguration setURLSchemeHandler:forURLScheme:]

Updated answer for iOS 11 and macOS 10.13

Since iOS 11 it is possible to create a subclass of WKURLSchemeHandler and register it in the WKWebView configuration: -[WKWebViewConfiguration setURLSchemeHandler:forURLScheme:].

WKWebView 发出请求并将内容呈现在进程外,这意味着您的应用无法听到他们发出的请求。如果您缺少某项功能,现在是时候向Apple打开错误报告和/或增强请求了。

WKWebView makes requests and renders content out-of-process, meaning your app does not hear the requests they make. If you are missing a functionality, now is the time to open a bug report and/or an enhancement request with Apple.

从iOS 10.3 SDK开始, WKWebView 仍然无法使用公共API使用自定义 NSURLProtocol

As of iOS 10.3 SDK, WKWebView is still unable to make use of custom NSURLProtocols using public APIs.

有进取心的开发人员找到了一个有趣的方法:
+ [WKBrowsingContextController registerSchemeForCustomProtocol:]
它应该添加提供了一个自定义协议处理方案列表的方案,然后应该使用 NSURLProtocol

Enterprising developers have found an interesting method: +[WKBrowsingContextController registerSchemeForCustomProtocol:] It supposedly adds the provided scheme to a list of custom protocol handled schemes and should then work with NSURLProtocol.

这篇关于WKWebView和NSURLProtocol无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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