The certificate for this server is invalid

前端 未结 9 1886
花落未央
花落未央 2021-02-13 18:04

I know that if I use following nsurlconnectiondelegate it will be fixed

– connection:willSendRequestForAuthenticationChallenge: – connection:canAuthenti

9条回答
  •  無奈伤痛
    2021-02-13 18:46

    The webserver which you are using is asking for Server Trust Authentication, you need to properly respond with the appropriate action. You need to implement connection:willSendRequestForAuthenticationChallenge: delegate method and use SecTrustRef to authenticate it.

    More information can be found here:- https://developer.apple.com/library/ios/technotes/tn2232/_index.html

    This was my code to fix error:

    - (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
    {
        NSURLProtectionSpace *protectionSpace = [challenge protectionSpace];
    
        id sender = [challenge sender];
    
        if ([[protectionSpace authenticationMethod] isEqualToString:NSURLAuthenticationMethodServerTrust])
        {
            SecTrustRef trust = [[challenge protectionSpace] serverTrust];
    
            NSURLCredential *credential = [[NSURLCredential alloc] initWithTrust:trust];
    
                [sender useCredential:credential forAuthenticationChallenge:challenge];
        }
        else
        {
            [sender performDefaultHandlingForAuthenticationChallenge:challenge];
        }
    }
    

提交回复
热议问题