【问题标题】:NSURLSession/NSURLConnection HTTP load failed when trying to download images with Alamofire尝试使用 Alamofire 下载图像时,NSURLSession/NSURLConnection HTTP 加载失败
【发布时间】:2017-06-22 14:11:19
【问题描述】:

我正在尝试使用 SSL 从 URL 将平铺图像下载到 GMSTileLayer。

GMSTileLayer 包含一个将瓦片加载到谷歌地图图层中的委托方法:

override func requestTileFor(x: UInt, y: UInt, zoom: UInt, receiver: GMSTileReceiver) {
    let url = URL(string: "\(urlPrefix)x=\(x)&y=\(y)&z=\(zoom)&is2d=t")
    let zoom = UInt((self.map?.camera.zoom)!)

    Alamofire.request(url!).responseImage { response in
        if let image = response.result.value {
            receiver.receiveTileWith(x: x, y: y, zoom: zoom, image: image)
        }
    }
}

调用此函数时,我收到以下错误消息:

2017-06-22 09:55:49.192 PPGaugeApp[78556:4886424] NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9801)
2017-06-22 09:55:49.274328-0400 PPGaugeApp[78556:4886488] [] nw_coretls_read_one_record tls_handshake_process: [-9801]

我已经通过在浏览器中的测试验证了这些网址正在返回图像。

在研究此错误时,几乎所有帖子都建议对 plist 进行一些更改,以至少包含以下内容:

NSAllowsArbitraryLoads

我目前的plist设置如下:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
    <key>NSExceptionDomains</key>
    <dict>
        <key>someDomain.com</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSExceptionRequiresForwardSecrecy</key>
            <true/>
            <key>NSExceptionMinimumTLSVersion</key>
            <string>TLSv1.2</string>
            <key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
            <false/>
            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
            <true/>
            <key>NSThirdPartyExceptionMinimumTLSVersion</key>
            <string>TLSv1.2</string>
            <key>NSRequiresCertificateTransparency</key>
            <false/>
        </dict>
    </dict>
</dict>

plist 中的任何内容似乎都不会对此错误消息产生任何影响。我们应用程序中的其他类使用 https 进行身份验证没有问题,但是这是我们通过 https url 下载文件的唯一地方。

还有其他地方我们应该检查吗?谢谢!

【问题讨论】:

    标签: ios google-maps alamofire alamofireimage


    【解决方案1】:

    由于 url 字符串 url 对话中存在一定百分比的转义字符而出错。使用下面的字符串扩展从字符串中获取 url。

    extension String {
        var getUrl: URL? {
            let strurl = (self as NSString).addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)!
            return URL(string: strurl)
         }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-09-04
      • 1970-01-01
      • 2016-12-19
      • 2016-10-19
      • 2016-01-03
      • 2018-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多