美文网首页
iOS wkwebview 忽略不受信任的https证书

iOS wkwebview 忽略不受信任的https证书

作者: 贝勒老爷 | 来源:发表于2019-09-23 17:25 被阅读0次
    #import "ViewController.h"
    #import <WebKit/WebKit.h>
    #import "NSURLRequest+DummyInterface.h"
    
    @interface ViewController ()<WKUIDelegate,WKNavigationDelegate,NSURLConnectionDataDelegate>
    
    @property(nonatomic,strong)WKWebView *wkWebView;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        
    
    
        
        WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
        self.wkWebView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:theConfiguration];
     
        
        self.wkWebView.navigationDelegate = self;
        self.wkWebView.UIDelegate = self;
        
        // 2.加载网页
    //    NSString *indexPath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
    //    NSString *appHtml = [NSString stringWithContentsOfFile:indexPath encoding:NSUTF8StringEncoding error:nil];
    //    NSURL *baseUrl = [NSURL fileURLWithPath:indexPath];
    //    [self.wkWebView loadHTMLString:appHtml baseURL:baseUrl];
        
    
        
    //    NSURL *url= [NSURL URLWithString:@"https://kyfw.12306.cn/otn/regist/init"];
        
        
    //    NSURL *url= [NSURL URLWithString:@"http://www.jianshu.com/u/2f02e0bb6464"];
        
          NSURL *url= [NSURL URLWithString:@"http://106.37.173.33:8008/webtrade/trade/tradelogin.html?qdid=zjzx"];
        
        
        
        
        NSURLRequest *req = [[NSURLRequest alloc]initWithURL:url];
        
        [self.wkWebView loadRequest:req];
        
        [self.view addSubview:self.wkWebView];
        
    }
    
    
    - (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler{
        
        if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
            
            NSURLCredential *credential = [[NSURLCredential alloc]initWithTrust:challenge.protectionSpace.serverTrust];
            
            completionHandler(NSURLSessionAuthChallengeUseCredential,credential);
            
        }
        
        
        
    }
    - (nullable WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures{
        
        
        if (!navigationAction.targetFrame.isMainFrame) {
            [self.wkWebView loadRequest:navigationAction.request];
        }
        
        return nil;
    }
    
    
    
    
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        
        
    }
    
    
    @end
    

    相关文章

      网友评论

          本文标题:iOS wkwebview 忽略不受信任的https证书

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