亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

UIWebView查看自簽名網站(沒有私人API,不是NSURLConnection)

UIWebView查看自簽名網站(沒有私人API,不是NSURLConnection)

人到中年有點甜 2019-08-30 17:25:22
有很多問題要問:我可以UIWebView查看自簽名的HTTPS網站嗎?答案總是涉及:使用私人api調用NSURLRequest:allowsAnyHTTPSCertificateForHost使用NSURLConnection代替和委托canAuthenticateAgainstProtectionSpace等對我來說,這些都不行。(1) - 表示我無法成功提交到應用商店。(2) - 使用NSURLConnection意味著在加載初始HTML頁面后必須從服務器獲取的CSS,圖像和其他內容。有誰知道如何使用UIWebView查看自簽名的https網頁,這不涉及上述兩種方法?或者 - 如果使用NSURLConnectioncan實際上可以用來渲染一個完整的CSS,圖像和其他所有的網頁 - 這將是偉大的!干杯,拉伸。
查看完整描述

3 回答

?
PIPIONE

TA貢獻1829條經驗 獲得超9個贊

終于我明白了!


你能做的是:


UIWebView正常啟動您的請求。然后 - 在webView:shouldStartLoadWithRequest- 我們回復NO,而是使用相同的請求啟動NSURLConnection。


使用NSURLConnection,您可以與自簽名服務器通信,因為我們能夠通過額外的委托方法來控制身份驗證UIWebView。因此,使用connection:didReceiveAuthenticationChallenge我們可以對自簽名服務器進行身份驗證。


然后,在connection:didReceiveData,我們取消NSURLConnection請求,并使用UIWebView- 現在可以工作,再次啟動相同的請求,因為我們已經通過服務器身份驗證:)


以下是相關的代碼段。


注意:您將看到的實例變量具有以下類型: 

UIWebView *_web

NSURLConnection *_urlConnection

NSURLRequest *_request


(我使用實例var,_request因為在我的情況下,它是一個包含大量登錄詳細信息的POST,但如果需要,您可以更改為使用傳入的請求作為方法的參數。)


#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];

}

我希望這能幫助其他人解決我遇到的同樣問題!


查看完整回答
反對 回復 2019-08-30
  • 3 回答
  • 0 關注
  • 641 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號