您的位置:首页 > 理论基础 > 计算机网络

iOS 设置UIWebView允许请求https网络请求

2016-09-18 09:25 489 查看
不使用AFNetworking的前提下, UIWebView默认不加载https协议的请求

 - (void)viewDidLoad {

     [super viewDidLoad];

     

     [self.navigationController setNavigationBarHidden:YES animated:YES];

     self.webView= [[UIWebView alloc] initWithFrame:self.view.bounds];

     [self.view addSubview:self.webView];

    _request = [NSMutableURLRequest requestWithURL:self.webUrl];

    NSURLConnection *conn = [NSURLConnection connectionWithRequest:_request delegate:self];

 }

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{

    if ([challenge previousFailureCount]== 0) {

        //NSURLCredential 这个类是表示身份验证凭据不可变对象。凭证的实际类型声明的类的构造函数来确定。

        NSURLCredential* cre = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];

        [challenge.sender useCredential:cre forAuthenticationChallenge:challenge];

    }

}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{

    [self.webView loadRequest:_request];

    [connection cancel];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: