Ignoring certificate errors with NSURLConnection

前端 未结 6 1385
臣服心动
臣服心动 2020-11-30 08:42

I am getting this error

The certificate for this server is invalid. You might be connecting to a server
that is pretending to be \"server addres goes here\"         


        
6条回答
  •  清歌不尽
    2020-11-30 09:16

    The correct (non deprecated, non private) way using the new:

    - (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
    

    method that apple has specified should be used for NSURLConnectionDelegates is to respond to the ServerTrust method with the credential that was provided for the protection space (this will allow you to connect:

    - (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
    {
        if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
        {
            NSLog(@"Ignoring SSL");
            SecTrustRef trust = challenge.protectionSpace.serverTrust;
            NSURLCredential *cred;
            cred = [NSURLCredential credentialForTrust:trust];
            [challenge.sender useCredential:cred forAuthenticationChallenge:challenge];
            return;
        }
    
        // Provide your regular login credential if needed...
    }
    

    This method gets called multiple times once for each available authentication method, if you don't want to login using that method use:

     [challenge.sender rejectProtectionSpaceAndContinueWithChallenge:challenge];
    

提交回复
热议问题