2014-01-17 58 views
1

如果主機位於可信列表中,我希望允許使用NSURLConnection的自簽名證書。使用NSURLConnection處理自簽名證書

我看到很多人做着這樣的:

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace { 
    if ([protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) { 
     if (allowSelfSignedCertForThisHost) { 
      NSLog(@"Allowing self signed!"); 
      return YES; 
     } 
    } 
    return NO; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { 
    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) { 
     if ([trustedHosts containsObject:challenge.protectionSpace.host]) { 
      [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge]; 
     } 
    } 
    [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge]; 
} 

不過,我想知道你爲什麼會調用useCredential:forAuthenticationChallenge而且continueWithoutCredentialForAuthenticationChallenge之後。

回答

1

只需在設備上使用iPhone Configuration UtilityApple Configurator添加該證書即可。

+0

這不可行 - 它是由我連接的主機發送的。 –

+0

@將會下載並安裝。你不需要私鑰。只需證書就足夠了。 –

+0

是否會有任何代碼更改來識別此自定義證書?這看起來更簡單,更安全。 –