尝试使用Alamofire下载图像时,NSURLSession / NSURLConnection HTTP加载失败

时间:2017-06-22 14:11:20

标签: ios google-maps alamofire alamofireimage

我正在尝试从带有SSL的网址将平铺图像下载到GMSTileLayer。

GMSTileLayer包含一个委托方法,可将切片加载到Google地图图层中:

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网址下载文件的唯一地方。

我们还有其他地方需要检查吗?谢谢!

1 个答案:

答案 0 :(得分:1)

由于url字符串url对话中出现的某些百分比转义字符出错。使用以下字符串扩展名从字符串中获取url。

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