为了账号安全,请及时绑定邮箱和手机立即绑定

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 关注
  • 601 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信