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

Reh*_*ani 4 networking parse-platform swift

我收到错误: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)
            }
        })
    }
}
Run Code Online (Sandbox Code Playgroud)

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

我不知道出了什么问题,所以所有答案都值得赞赏.

Seb*_*ddd 10

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

<key>NSAppTransportSecurity</key>  
<dict>  
   <key>NSAllowsArbitraryLoads</key>  
   <true/>  
</dict> 
Run Code Online (Sandbox Code Playgroud)

在iOS9中,ATS在网络呼叫期间实施最佳实践,包括使用HTTPS.在Apple文档中阅读更多相关信息.


Lep*_*ron 5

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

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

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

所以基本上你的Info.plist应该是这样的,并包括域.

注意:为了更好的透明度,我还重新声明了默认值为NSAllowsArbitraryLoadsfalse

<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>
Run Code Online (Sandbox Code Playgroud)

最好的祝福.

万一,你只是不关心所有这些混乱(我不推荐这个),并且只想调试你的用户界面,你也可以暂时使用非默认的App TransportSecurity并允许任何东西:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>
Run Code Online (Sandbox Code Playgroud)

我不确定,苹果是否会让这个通过AppStore评论;-)

顺便说一句:通过查看已建立的连接来跟踪应用程序的所有连接.您现在可以嗅探应用程序的流量,这是由第三方工具生成的,或者您使用记录所有网络流量,在此处引用:如何确定哪个URL被App Transport Security阻止?

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

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

nmap --script ssl-enum-ciphers -p 443 your-domain-without-https.com
Run Code Online (Sandbox Code Playgroud)