2015-10-01 39 views
0

我嘗試通過服務器獲取圖像時出現此錯誤。
這裏是我的代碼,通過服務器上獲取圖像:ios 9中的NSURLSession/NSURLConnection HTTP加載失敗(kCFStreamErrorDomainSSL,-9802)

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

之前,我在計算器問這個問題,我已經嘗試在我的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給我下面的錯誤,當我啓動應用程序:

應用傳輸安全性已阻止明文HTTP(HTTP://)資源 負載,因爲它是不安全的。臨時例外可以通過 您的應用程序的Info.plist文件進行配置。

請幫助我,因爲我已經花了很多時間來解決這個問題。
在此先感謝。

+0

在plist中加入NSAllowsArbitraryLoads後,你應該嘗試刪除應用程序,並刪除Xcode中導出的數據文件夾,乾淨,運行項目... 。!它爲我工作..! –

+0

不工作的傢伙。 –

回答

2

如果您出於某種原因偏好使用NSAllowsArbitraryLoads鍵來忽略安全性限制,您必須將其放在NSAppTransportSecurity字典中,因爲該字典只是將您的字典置於NSAppTransportSecurity鍵之下。

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

但是,它不被推薦,並可能導致蘋果拒絕你的應用程序。 蘋果很清楚,他們打算拒絕使用這個標誌的應用程序沒有特定的原因。

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

參考: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> 
+0

我已經試過這個,但仍然沒有任何其他建議? –

+0

請檢查我更新的問題。我的plist有問題嗎? –

+0

它不是「你應該」,它的「你可以暫時作爲解決方法」。但肯定不是「應該」。這個詞應該暗示這是人們必須做的推薦做法。 – Gruntcakes

相關問題