3 回答

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超9個(gè)贊
終于我明白了!
你能做的是:
UIWebView正常啟動(dòng)您的請求。然后 - 在webView:shouldStartLoadWithRequest- 我們回復(fù)NO,而是使用相同的請求啟動(dòng)NSURLConnection。
使用NSURLConnection,您可以與自簽名服務(wù)器通信,因?yàn)槲覀兡軌蛲ㄟ^額外的委托方法來控制身份驗(yàn)證UIWebView。因此,使用connection:didReceiveAuthenticationChallenge我們可以對自簽名服務(wù)器進(jìn)行身份驗(yàn)證。
然后,在connection:didReceiveData,我們?nèi)∠鸑SURLConnection請求,并使用UIWebView- 現(xiàn)在可以工作,再次啟動(dòng)相同的請求,因?yàn)槲覀円呀?jīng)通過服務(wù)器身份驗(yàn)證:)
以下是相關(guān)的代碼段。
注意:您將看到的實(shí)例變量具有以下類型:
UIWebView *_web
NSURLConnection *_urlConnection
NSURLRequest *_request
(我使用實(shí)例var,_request因?yàn)樵谖业那闆r下,它是一個(gè)包含大量登錄詳細(xì)信息的POST,但如果需要,您可以更改為使用傳入的請求作為方法的參數(shù)。)
#pragma mark - Webview delegate
// Note: This method is particularly important. As the server is using a self signed certificate,
// we cannot use just UIWebView - as it doesn't allow for using self-certs. Instead, we stop the
// request in this method below, create an NSURLConnection (which can allow self-certs via the delegate methods
// which UIWebView does not have), authenticate using NSURLConnection, then use another UIWebView to complete
// the loading and viewing of the page. See connection:didReceiveAuthenticationChallenge to see how this works.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
{
NSLog(@"Did start loading: %@ auth:%d", [[request URL] absoluteString], _authenticated);
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;
[_web 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];
}
我希望這能幫助其他人解決我遇到的同樣問題!
- 3 回答
- 0 關(guān)注
- 640 瀏覽
添加回答
舉報(bào)