Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 允许在WKWebView中使用未验证的ssl证书_Ios_Ssl_Uiwebview_Self Signed_Wkwebview - Fatal编程技术网

Ios 允许在WKWebView中使用未验证的ssl证书

Ios 允许在WKWebView中使用未验证的ssl证书,ios,ssl,uiwebview,self-signed,wkwebview,Ios,Ssl,Uiwebview,Self Signed,Wkwebview,我正在尝试在iOS 8的WKWebView中加载带有自签名证书的HTTPS url,但一直失败。UIWebView使用的变通方法(使用来自NSUrlRequest的SetAllowsAnyHttpSCCertificate)似乎不起作用。有人知道有什么解决办法吗 我不需要对AppStore有效的解决方案,因为我只需要在开发阶段访问自签名证书站点,而不需要在生产阶段访问,但这对于开发和测试服务器实例来说确实是个问题 提前感谢。在设备上的Safari中打开URL一次,系统会提示您选择接受证书。一旦被

我正在尝试在iOS 8的WKWebView中加载带有自签名证书的HTTPS url,但一直失败。UIWebView使用的变通方法(使用来自NSUrlRequest的SetAllowsAnyHttpSCCertificate)似乎不起作用。有人知道有什么解决办法吗

我不需要对AppStore有效的解决方案,因为我只需要在开发阶段访问自签名证书站点,而不需要在生产阶段访问,但这对于开发和测试服务器实例来说确实是个问题


提前感谢。

在设备上的Safari中打开URL一次,系统会提示您选择接受证书。一旦被接受,它也应该在你的应用程序中工作,因为该证书现在已为设备所知。
这是一个针对每台设备的解决方案,在发布时不会影响您的应用。

看起来在这个阶段可能没有解决方案(iOS 8.1.1)。人们可能期望WKNavigationDelegate方法
webView:didReceiveAuthenticationChallenge:completionHandler:
来处理此问题,但基于此,一名苹果员工确认,当遇到自签名证书时,当前不会调用此委托方法。

这在iOS 9中已修复
WKWebView
最后在
WKNavigationDelegate
上调用
webView(uU2;:didReceiveAuthenticationChallenge:completionHandler:)
。不幸的是,如果您在iOS 8设备上运行Xcode 7内置的代码(至少在我的初始测试中不是这样),那么这就不起作用了

在我下面的例子中,我实际上并没有对cert做任何事情,只是让它通过,而不做任何进一步的验证(显然是一个糟糕的生产代码计划)。请参阅(清单3),了解他们希望您在这里做什么的更多细节

斯威夫特:

func webView(webView: WKWebView, didReceiveAuthenticationChallenge challenge: NSURLAuthenticationChallenge,
    completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential?) -> Void) {
        let cred = NSURLCredential.init(forTrust: challenge.protectionSpace.serverTrust!)
        completionHandler(.UseCredential, cred)
}
Swift 3:

let cred = URLCredential(trust: challenge.protectionSpace.serverTrust!)
completionHandler(.useCredential, cred)
Swift 4:

func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
    let cred = URLCredential(trust: challenge.protectionSpace.serverTrust!)
    completionHandler(.useCredential, cred)
}
目标-C

NSURLCredential * credential = [[NSURLCredential alloc] initWithTrust:[challenge protectionSpace].serverTrust];
completionHandler(NSURLSessionAuthChallengeUseCredential, credential);

我有同样的错误,并尝试使用上面投票最多的答案来解决它,我使用下面的代码创建了一个
NSURLCredential
对象,但失败了

NSURLCredential * credential = [[NSURLCredential alloc] initWithTrust:[challenge protectionSpace].serverTrust];
然后我找到了一个解决办法。这帮助了我:

- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler {
  NSLog(@"Allow all");
  SecTrustRef serverTrust = challenge.protectionSpace.serverTrust;
  CFDataRef exceptions = SecTrustCopyExceptions (serverTrust);
  SecTrustSetExceptions (serverTrust, exceptions);
  CFRelease (exceptions);
  completionHandler (NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:serverTrust]);
  }

作为一个ios新手,我花了很多时间来研究这个问题,我认为提出的解决方案都不完整。以下是我为使WKWebView在我的案例中发挥作用所做的工作(非常简单的web视图,只需要访问开发人员的自签名证书):

第一件事:在我的root Info.plist文件中,我添加了“应用程序传输安全设置”作为字典,并添加了“允许任意加载”项,值为YES

第二:我将这段代码添加到我的ViewController(继承了UIViewController和WKNavigationDelegate)——这段代码来源于其他地方的几个答案

func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
    guard let serverTrust = challenge.protectionSpace.serverTrust else { return completionHandler(.useCredential, nil) }
    let exceptions = SecTrustCopyExceptions(serverTrust)
    SecTrustSetExceptions(serverTrust, exceptions)
    completionHandler(.useCredential, URLCredential(trust: serverTrust))
}
请注意,此解决方案可能会被应用商店拒绝-我将向应用商店提交“允许任意加载”项,该项的值为NO.

请尝试以下操作:

 func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {

    if let serverTrust = challenge.protectionSpace.serverTrust {
        let credential = URLCredential(trust: serverTrust)
        completionHandler(.useCredential, credential)
    }else{
         completionHandler(.useCredential, nil)
    }

}

iOS 10+忽略
NSAllowsArbitraryLoads
:检查。另外,对其他标志要谨慎,因为苹果将要求AppStore审查的理由。如果忽略与UIWebview相关的解决方案,苹果无论如何都会拒绝

简单的方法
  • 在设备上下载自签名根CA
  • 在设置>关于>中信任它。。。证书
  • 这显然不适合大量的目标受众

    更好但更难的方法
  • 阅读
  • 实现WKNavigationDelegate
    webView(uuxReceive:completionHandler:)
  • 关于websocket的更多信息(OSError-9807) 这是一种不同的身份验证路由,因此目前无法覆盖系统默认行为。苹果论坛帖子:


    这很奇怪-您在“常规”>“配置文件”部分的“设备设置”中看到证书了吗?您能告诉我如何使用此代码吗?我完全是swift的新手,我已经覆盖了
    viewDidLoad
    函数,并使用:
    webView.loadRequest(NSURLRequest(url:url))
    加载了我的url,但我实际如何使用你的函数呢?再次抱歉,如果答案是显而易见的,您需要为WKWebView提供一个实现
    WKNavigationDelegate
    的类。在
    WKNavigationDelegate
    中,您可以覆盖上述方法。在该方法中,您将需要类似于我上面所述的内容来允许请求通过(假设您希望这样做)。在我的示例中,challenge.protectionSpace.serverTrust解析为nil。应用程序crashHi@jvoll,很棒的帖子。您是否可以使用Swift 5中的
    WKWebView
    ,改进您的文章,了解SwiftUI的情况?如何使此代码有条件。我的意思是,我需要为无效的ssl证书网站显示一个警报,如果用户说无论如何访问,那么只加载这样的网站。请告诉我怎么做?对我有用。根据苹果公司的说法,最好在SecTrustSetExceptions()之后调用SecTrustEvaluateWithError(::)。将异常从
    serverTrust
    复制到
    serverTrust
    实现了什么?