如何iOS7

问题描述:

处理NSURLAuthenticationChallenge我想发布的东西到具有SSL问题的Web服务。 我用下面的方法:如何iOS7

NSURLConnection * urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES]; 

应该立即开始发送数据已在请求被设置为; 但服务具有安全性的问题,它不能正常工作。但是我想发送数据并且想要忽略安全问题;所以我用以下NSURLConnectionDelegate的方法:

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace { 
    return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]; 
} 

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

    [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge]; 
} 

但他们已被弃用。我如何处理安全问题并告知将数据传递给Web服务而不考虑它?

你应该使用willSendRequestForAuthenticationChallenge这样的。

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge 
{ 
    self.challenge = challenge; 
    [self askUserAcceptSSLError]; 
} 

- (void)askUserAcceptSSLError 
{ 
    // Ask user like UIAlertView or so. 
    // Put these responses in UIAlertView delegate ... 

    // If User accepts (or force this if you want ignore SSL certificate errors): 
    [[self.challenge sender] 
     useCredential:[NSURLCredential credentialForTrust:self.challenge.protectionSpace.serverTrust] 
     forAuthenticationChallenge:self.challenge]; 
    [[self.challenge sender] continueWithoutCredentialForAuthenticationChallenge:self.challenge]; 

    // If User deny request: 
    [[self.challenge sender] cancelAuthenticationChallenge:self.challenge]; 
}