NSURLSession / NSURLConnection HTTP加载失败(kCFStreamErrorDomainSSL,-9802)

时间:2015-06-18 23:13:25

标签: swift networking parse-platform

我收到错误:NSURLSession / NSURLConnection HTTP加载失败(kCFStreamErrorDomainSSL,-9802),我怀疑这是因为查询来自Parse的图像。这是我查询的方法:

func fetchImageForEmployee(employee: PFEmployee, completion: (error: String?, image: UIImage?) -> Void) {
    if (employee.profilePicture == nil) {
        completion(error: "No image file", image: nil)
    } else {
        employee.profilePicture!.getDataInBackgroundWithBlock({ (data, error) -> Void in
            if let error = error {
                let errorString = error.userInfo["error"] as? String
                completion(error: errorString, image: nil)
            } else if (data != nil) {
                let image = UIImage(data: data!)
                completion(error: nil, image: image)
            } else {
                completion(error: nil, image: nil)
            }
        })
    }
}

我还打印了错误,这是调试器中出现的内容:不允许在取消分配时尝试加载视图控制器的视图,并且可能导致未定义的行为(UISearchController:0x13ee44dc0)

我不知道出了什么问题,所以所有的答案都很受欢迎。

2 个答案:

答案 0 :(得分:10)

右键单击Info.plist文件&gt;打开作为&gt;源代码,并在最新</dict>之前添加以下内容:

<key>NSAppTransportSecurity</key>  
<dict>  
   <key>NSAllowsArbitraryLoads</key>  
   <true/>  
</dict> 

在iOS9中,ATS在网络呼叫期间实施最佳实践,包括使用HTTPS。请在Apple Documentation

中详细了解相关信息

答案 1 :(得分:5)

我建议不要允许任何事情。

您需要在构建目标的Info.Plist中定义要应用这些规则的URL。

您可以在Apple的文档页面上找到正确的声明: https://developer.apple.com/library/prerelease/ios/technotes/App-Transport-Security-Technote/

所以基本上你的Info.plist看起来应该是这样的并包含域名。

注意:为了获得更好的透明度,我还将NSAllowsArbitraryLoads的默认值重新声明为false

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <false/>
    <key>NSExceptionDomains</key>
    <dict>
        <key>yourdomain.com</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSAllowsArbitraryLoads</key>
            <true/>
            <key>NSTemporaryExceptionMinimumTLSVersion</key>
            <string>TLSv1.1</string>
            <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
            <true/>
        </dict>
    </dict>
</dict>

最好的问候。

如果你只是不关心所有这些麻烦(我不推荐这个)并且只想调试你的UI,你可以选择暂时使用非默认的App TransportSecurity和只允许任何事情:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

我不确定,Apple是否会在AppStore Review中传递此内容; - )

顺便说一句:通过查看已建立的连接来跟踪应用程序的所有连接。您现在可以嗅探由第三方工具生成的应用流量,或者您使用记录所有网络流量,在此处引用:How can I figure out which URL is being blocked by App Transport Security?

很容易跟踪此日志中发生的所有错误(不太难以查找错误代码)。通过这种方式,由于负载限制(当然,优秀的软件工程师心里明白;),我很容易看到正在建立的连接并且可能失败了。)) 要访问日志,您可以使用例如iFunBox或其他东西,如果它在您的移动设备上。

简短提示:要检查服务器运行的TLS版本,我正在使用nmap:

nmap --script ssl-enum-ciphers -p 443 your-domain-without-https.com
相关问题