ios 自簽證書,webview加載https

首先如果你的證書是自簽的,那么你的webview是是不是不能加載https的網(wǎng)址。如果是,且出現(xiàn)了如下的問題。那么就請接著看下去.

解決這個問題的辦法,代碼如下:

BOOL _Authenticated;

NSURLRequest * _FailedRequest; (兩個全局變量)

- (void)createWebView {

_webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];

_webview.delegate = self;

[self.view addSubview:_webview];

NSString * string = [NSString stringWithFormat:@"%@%@",baseRequest, self.urlString];

NSURL * url = [NSURL URLWithString:string];

_FailedRequest = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:30.f];

[_webview loadRequest:_FailedRequest];

}

#pragma UIWebViewDelegate

- (void)webViewDidStartLoad:(UIWebView *)webView {

NSLog(@"webview開始加載!");

}

- (void)webViewDidFinishLoad:(UIWebView *)webView {

NSLog(@"webview加載完成!");

}

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request? navigationType:(UIWebViewNavigationType)navigationType {

BOOL result = _Authenticated;

if (!_Authenticated) {

_FailedRequest = request;

NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

[urlConnection start];

}

return result;

}

#pragma NSURLConnectionDelegate

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

if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {

NSString * string = [NSString stringWithFormat:@"%@%@",baseRequest, self.urlString];

NSURL* baseURL = [NSURL URLWithString:string];

if ([challenge.protectionSpace.host isEqualToString:baseURL.host]) {

[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];

}

}

[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];

}

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

_Authenticated = YES;

[connection cancel];

[_webview loadRequest:_FailedRequest];

}。

到這里,運(yùn)行你的demo試試吧,奇跡會發(fā)生!

以上是參考了alstonwei在github上的一個demo的,demo的鏈接是https://github.com/alstonwei/UIWebView-HTTPS-Test。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容