首页 > 解决方案 > NSURLConnection 以错误 1200 结束,现在是 1202?

问题描述

我所有的 api 调用都运行良好,但升级Xcode到最新版本Xcode 9.4.1后,我的 api 调用都不起作用。

我第一次收到NSURLConnection finished with error - code -1200,但在添加后:

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

到了info.plist,我现在收到了NSURLConnection finished with error - code -1202SSL Certificate我通过手动连接并检查(A+)确认我正在连接的域没有问题。

这是我用于加载图像的代码:

if let pictureUrl = URL(string: url) {
    do {
        let data = try Data(contentsOf: pictureUrl)
        img.image = UIImage(data: data)
        if let imageWidth = (img.image?.size.width) {
            let height = width / imageWidth * (img.image?.size.height)!
            conHeight.constant = height
        }
    }
    catch let error {
        print("Error \(error.localizedDescription)")
    }
}

标签: iosswiftxcodeurl

解决方案


写入setenv("CFNETWORK_DIAGNOSTICS", "3", 1)后:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

正如@Jack 建议的那样,我解决了这个问题。我有与Charles跑步有关的错误。关闭 Charles 后,我的 api 调用现在可以工作了。我可能只需要更新正在使用的SSL CertificateCharles因为error 1202是 SSL 证书错误。


推荐阅读