NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802) in ios 9

962 Views Asked by At

I got this error when I try to fetch images through server.
Here is my code for fetch image through server:

-(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);
        });
    }];
}  

Before I ask this question in stackoverflow I already try to add this in my info.plist. Here is my 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> 

But it still not working.
IT give me following error when I launch the app:

App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.

Please help me because I already take two much of time to solve this issue.
Thanks in Advance.

1

There are 1 best solutions below

4
teamnorge On

If you are for some reasons prefer to use NSAllowsArbitraryLoads key to ignore security restrictions you must place it inside NSAppTransportSecurity dictionary, for that just put your dict under NSAppTransportSecurity key.

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

However it's not recommended and may cause to a rejection of your app by Apple. Apple is very clear in that they intend to reject apps who use this flag without a specific reason.

The better solution could be to add an exception to a specific domain.

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

Your NSAppTransportSecurity section then may look like:

<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>