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


//
//  ViewController.m
//  wkweb
//
//  Created by 柏超曾 on 2017/8/29.
//  Copyright © 2017年 柏超曾. All rights reserved.
//

#import "ViewController.h"
#import 
#import "NSURLRequest+DummyInterface.h"

@interface ViewController ()

@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证书)