iOS ATS测试跳过无效证书

来源:互联网 发布:怎样在淘宝联盟买东西 编辑:程序博客网 时间:2024/05/04 18:48
1.   

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{        NSString* url = [[request URL] absoluteString];//判断是不是https    if ([url containString:@"https"]) {        //如果是https:的话,那么就用NSURLConnection来重发请求。从而在请求的过程当中吧要请求的URL做信任处理。        if (!self.isAuthed) {            _isAuthed = kNeverAuthenticate;            _originRequest = request;            NSURLConnection* conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];            [conn start];            [webView stopLoading];            return NO;        }     }        return YES;}

2.
#pragma mark connection- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{    NSLog(@"回调");     if ([challenge previousFailureCount] == 0){                self.isAuthed = kTryAuthenticate;                NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];                [challenge.sender useCredential:credential forAuthenticationChallenge:challenge];        [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];            } else{        [[challenge sender] cancelAuthenticationChallenge:challenge];    }}- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{        self.isAuthed = kTryAuthenticate;    //webview 重新加载请求。    [self.baseWebView loadRequest:_originRequest];    [connection cancel];}


3.该段代码加在AppDelegate.m的@end后面
@implementation NSURLRequest(DataController)+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host{    return YES;}@end


4.info.plist添加ATS的配置:  添加 App Transport Security Settings   --  Allow Arbitrary Loads  --  YES






原创粉丝点击