根據你的意見,你得到一個無效的證書錯誤,並且你正在使用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];
}
}
可以調用使用該設備的瀏覽器相同的網址是什麼? –
它給我一個錯誤:無法驗證服務器身份 –
你打開此網址在網絡視圖或使用它來使用NSURLSession或NSURLConnection類進行網絡API調用?如果可以的話,你也介意分享完整的網址嗎?看起來您需要處理身份驗證挑戰,因爲您看到的錯誤是由於身份不明的證書。在這種情況下,您必須實現在連接期間調用的某些委託方法,並相應地處理它們。 –