Ignoring certificate errors with NSURLConnection

前端 未结 6 1380
臣服心动
臣服心动 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:14

    I had similar issue. Got it solved by using below code snippet :

    -(void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
    
        NSLog(@"This will execute successfully!");
        if ([[challenge protectionSpace] authenticationMethod] == NSURLAuthenticationMethodServerTrust) {
    
            [[challenge sender] useCredential:[NSURLCredential credentialForTrust:[[challenge protectionSpace] serverTrust]] forAuthenticationChallenge:challenge];
        }
    }
    

    Since below methods are deprecated :

    - (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)space { ... }
    
    - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { ... }
    

提交回复
热议问题