美文网首页iOS 开发iOS Developer
iOS HTTPS 自检证书请求服务器和WKWebView

iOS HTTPS 自检证书请求服务器和WKWebView

作者: 在这蓝色天空下 | 来源:发表于2017-01-09 15:52 被阅读132次

    1、使用AFN3.0 HTTPS网络请求

        +(AFSecurityPolicy *)customSecurityPolicy{     //简单封装一下
    
        //2 设置证书模式
        NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"mnchip" ofType:@"cer"];
    
        NSData *cerDat = [NSData dataWithContentsOfFile:cerPath];
    
        AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
    
        //允许自检证书
        securityPolicy.allowInvalidCertificates = YES;
    
        //域名与服务器一致
        securityPolicy.validatesDomainName = YES;
    
        securityPolicy.pinnedCertificates = [[NSSet alloc] initWithObjects:cerDat, nil];
    
        return securityPolicy;
    
        }
    

    在有网络请求的地方:

    ******
    
        AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
        //添加证书
        [session setSecurityPolicy:[OperationHelper customSecurityPolicy]];
    
    ******
    

    这样就可以了

    2、关于WKWebView , 我是将Allow Arbitrary Loads in Web Content 置为 yes,然后正常使用,注意一点,要实现这个代理方法

    
        - (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler{
    
        if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
    
        NSURLCredential *card = [[NSURLCredential alloc]initWithTrust:challenge.protectionSpace.serverTrust];
    
        completionHandler(NSURLSessionAuthChallengeUseCredential,card);
    
        }}
    
    
    @implementation NSURLRequest(DataController)
    + (BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host
    {
        return YES;
    }
    @end
    

    关于WKWebView就可以正常显示了

    3、UIWebView 访问https 绕过证书验证的方法

    在AppDelegate.m里面添加下面这些代码

    @implementation NSURLRequest(DataController)
    + (BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host
    {
        return YES;
    }
    @end
    

    相关文章

      网友评论

        本文标题:iOS HTTPS 自检证书请求服务器和WKWebView

        本文链接:https://www.haomeiwen.com/subject/rbkhbttx.html