ios 9中的NSURLSession / NSURLConnection HTTP加载失败(kCFStreamErrorDomainSSL,-9802)

时间:2015-10-01 14:11:11

标签: ios httprequest nsurlconnection xcode7.1beta ios9.1

当我尝试通过服务器获取图像时出现此错误 这是我通过服务器获取图像的代码:

-(void)downloadThumbnailWithHeight:(NSInteger)height width:(NSInteger)width callback:(void (^)(UIImage*))callback {
    NSString* key = [NSString stringWithFormat:@"%ldx%ld", (long)width, (long)height];
    UIImage* __block thumbnail = [_thumbnails objectForKey:key];
    if (thumbnail) { callback(thumbnail); return; }
    HttpRequest* request = [HttpRequest requestWithRelativePath:[NSString stringWithFormat:@"/api/v1/incident/%@/resize?height=%d&width=%d",self.uuid, (int)height, (int)width]];
    [HttpResponse processAsynchronousRequest:request onCompletion:^(HttpResponse* response) {
        dispatch_async(dispatch_get_main_queue(), ^{
            thumbnail = [UIImage imageWithData:response.responseData];
            if (!thumbnail) thumbnail = [UIImage imageNamed:@"gray_thumbnail_background.png"];
            [_thumbnails setObject:thumbnail forKey:key];
            callback(thumbnail);
        });
    }];
}  

在我在stackoverflow中提出这个问题之前,我已经尝试在我的info.plist中添加它。这是我的plsit:

    <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>
    <key>NSLocationAlwaysUsageDescription</key>
    <string>To Find out your location</string>
    <key>NSLocationWhenInUseUsageDescription</key>
    <string>To Find out your location</string>
    <key>CFBundleIdentifier</key>
    <string>com.abc.def</string>
    <key>CFBundleExecutable</key>
    <string>${EXECUTABLE_NAME}</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleShortVersionString</key>
    <string>1.0</string>
    <key>CFBundleVersion</key>
    <string>1</string>
    <key>CFBundleDevelopmentRegion</key>
    <string>en</string>
    <key>CFBundlePackageType</key>
    <string>BNDL</string>
    <key>CFBundleSignature</key>
    <string>????</string>
</dict>
</plist> 

但它仍无法正常工作 启动应用程序时,IT会给我以下错误:

  

App Transport Security已阻止明文HTTP(http://)资源   负载,因为它是不安全的。可以通过配置临时例外   你的应用程序的Info.plist文件。

请帮帮我,因为我已经花了两大部分时间来解决这个问题 在此先感谢。

1 个答案:

答案 0 :(得分:2)

如果由于某些原因,您更喜欢使用NSAllowsArbitraryLoads密钥来忽略安全限制,则必须将其放在NSAppTransportSecurity字典中,因为只需将您的字典放在NSAppTransportSecurity密钥下。

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

但不建议这样做,可能会导致Apple拒绝您的应用。 Apple非常清楚,他们打算拒绝使用此标志的应用程序,而无需特殊原因。

更好的解决方案可能是向特定域添加例外。

参考:https://developer.apple.com/library/prerelease/ios/technotes/App-Transport-Security-Technote/index.html

您的NSAppTransportSecurity部分可能如下所示:

<key>NSAppTransportSecurity</key>
<dict>
  <key>NSExceptionDomains</key>
  <dict>
     <key>yourdomain.com</key>
     <dict>
       <key>NSIncludesSubdomains</key>
       <true/>
       <key>NSExceptionAllowsInsecureHTTPLoads</key>
       <true/>
       <key>NSExceptionMinimumTLSVersion</key>
       <string>TLSv1.2</string>
     </dict>
   </dict>
</dict>