2016-03-14 68 views
0

當我使用http://domain.com時,我的應用工作正常。但今天,當我將http://更改爲https://時,我面臨的問題是:NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)。我也對.plist進行了更改。這裏是我的.plist代碼:NSURLConnection HTTP加載失敗(kCFStreamErrorDomainSSL,-9813)

<key>NSAppTransportSecurity</key> 
    <dict> 
     <key>NSAllowsArbitraryLoads</key> 
     <true/> 
     <key>NSExceptionDomains</key> 
     <dict> 
      <key>dev.domainName.in</key> 
      <dict> 
       <key>NSExceptionAllowsInsecureHTTPLoads</key> 
       <true/> 
       <key>NSIncludesSubdomains</key> 
       <true/> 
       <key>NSTemporaryExceptionMinimumTLSVersion</key> 
       <string>TLSv1.2</string> 
      </dict> 
     </dict> 
    </dict> 

不過還是我面對這個問題。請幫助我。我錯過了什麼?

+0

可以調用使用該設備的瀏覽器相同的網址是什麼? –

+0

它給我一個錯誤:無法驗證服務器身份 –

+0

你打開此網址在網絡視圖或使用它來使用NSURLSession或NSURLConnection類進行網絡API調用?如果可以的話,你也介意分享完整的網址嗎?看起來您需要處理身份驗證挑戰,因爲您看到的錯誤是由於身份不明的證書。在這種情況下,您必須實現在連接期間調用的某些委託方法,並相應地處理它們。 –

回答

0

從iOS 9開始,Apple已經執行了ATS。 Here是有關這方面的文檔。試着放鬆限制,看看它是否有效。

它適用於運行iOS 9之前的操作系統版本的設備嗎?

+0

我已閱讀本文檔並使用它們提供的代碼。但它仍然給我一個錯誤。 –

+0

請嘗試使用本網站檢查您的證書是否有任何問題。 https://www.sslshopper.com/ssl-checker.html –

+0

證書是自簽名的。除非將證書作爲可信證書手動添加到其Web瀏覽器,否則用戶在訪問此網站時會收到警告。您可以通過購買可信SSL證書來解決此錯誤 –

0

根據你的意見,你得到一個無效的證書錯誤,並且你正在使用NSURLConnection你的解決方案是實現這些委託。看看這是否解決了這個問題。

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge]; }

注意,你基本上是忽略SSL錯誤,並允許連接繼續進行。所以,如果你打開了黑客應用程序。爲了進行測試,您可以使用此功能,但請確保在生產環境中安裝適當的證書。

但是,如果你想正確處理這個問題,並在證書不正確的情況下警告用戶,你可以這樣做。

OSStatus err = noErr; 
BOOL trusted = NO; 
NSURLProtectionSpace * protectionSpace = challenge.protectionSpace; 
SecTrustRef serverTrustRef = protectionSpace.serverTrust; 
SecTrustResultType trustResult; 

//check if the server trust that we got from the server can be trusted by default 
err = SecTrustEvaluate(serverTrustRef, &trustResult); 
trusted = (err == noErr) && ((trustResult == kSecTrustResultProceed) || (trustResult == kSecTrustResultUnspecified)); 

if (trusted) //if the site is trusted then continue with that trust 
{ 
    [challenge.sender useCredential:[NSURLCredential credentialForTrust:protectionSpace.serverTrust] 
      forAuthenticationChallenge:challenge]; 
} 
else //if not then warn the user about this and let the user make a decision 
{ 
    //warn the user about the cert problem using a dialog with options to continue or ignore. 
    //Continue alert action call : [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] 
      forAuthenticationChallenge:challenge]; 
    //Dont continue action: [challenge.sender continueWithoutCredentialForAuthenticationChallenge:_challenge]; 
    //OR call: [sender cancelAuthenticationChallenge:challenge]; 
} 

}

+0

我應該在哪裏寫這段代碼? –

+0

在你的NSURLConnectionDelegate中。放一些NSLog調用來查看控件是否到達那裏供您參考。 –

+0

沒有委託被調用。 –

相關問題