今天后端同事給了我一個鏈接材部,https請求的,證書是假的唯竹,就是在網(wǎng)頁上打開顯示鏈接不安全乐导,讓我看看能不能在ios客戶端請求顯示出來。用webview一加載浸颓,果然不行物臂,報了一大段錯誤和詳情。
Error Domain=NSURLErrorDomain Code=-1202 "The certificate for this server is invalid.
You might be connecting to a server that is pretending to be “h5.opencredit.com” which could put your confidential information at risk."
UserInfo={NSURLErrorFailingURLPeerTrustErrorKey=<SecTrustRef: 0x6000001042f0>, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, _kCFStreamErrorDomainKey=3, _kCFStreamErrorCodeKey=-9843
嘗試了下产上,去掉s改成http請求棵磷,完全可以。
但是需要的還是https請求晋涣,故折騰了一下仪媒,找到方法,參考:http://stackoverflow.com/questions/11573164/uiwebview-to-view-self-signed-websites-no-private-api-not-nsurlconnection-i 谢鹊,直接上代碼算吩。
#import "ViewController.h"
@interface ViewController ()<UIWebViewDelegate,NSURLConnectionDelegate>
{
BOOL _authenticated;
NSURLConnection *_urlConnection;
NSURLRequest *_request;
}
@property(nonatomic,strong)UIWebView *webview;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.webview = [[UIWebView alloc]initWithFrame:self.view.bounds];
self.webview.delegate = self;
[self.view addSubview:self.webview];
_request = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:url]];
[self.webview loadRequest:_request];
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
NSLog(@"did finish url = %@",webView.request.URL);
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
NSLog(@"error = %@",error.description);
}
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if (!_authenticated) {
_authenticated = NO;
_urlConnection = [[NSURLConnection alloc] initWithRequest:_request delegate:self];
[_urlConnection start];
return NO;
}
return YES;
}
#pragma mark - NURLConnection delegate
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
{
NSLog(@"WebController Got auth challange via NSURLConnection");
if ([challenge previousFailureCount] == 0)
{
_authenticated = YES;
NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
[challenge.sender useCredential:credential forAuthenticationChallenge:challenge];
} else
{
[[challenge sender] cancelAuthenticationChallenge:challenge];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
{
NSLog(@"WebController received response via NSURLConnection");
// remake a webview call now that authentication has passed ok.
_authenticated = YES;
[_webview loadRequest:_request];
// Cancel the URL connection otherwise we double up (webview + url connection, same url = no good!)
[_urlConnection cancel];
}
// We use this method is to accept an untrusted site which unfortunately we need to do, as our PVM servers are self signed.
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end