使用UIWebView忽略无效的服务器证书

Kri*_*son 26 uiwebview ios

我们有一个iOS应用程序,它使用UIWebView来显示内容.我们使用如下代码的数据加载它:

NSURL *url = [NSURL URLWithString:myURLString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView setDelegate:self];
[_webView loadRequest:request];
Run Code Online (Sandbox Code Playgroud)

这通常适用于HTTP请求,但现在我们对具有自签名SSL证书的服务器使用HTTPS.运行以上操作时,webView:didFailLoadWithError:将调用委托方法,并显示以下错误:

此服务器的证书无效.您可能正在连接到假装是"blah.blah.blah.com"的服务器,这可能会使您的机密信息面临风险."

我想简单地忽略无效证书并继续处理请求,就像在Mobile Safari中一样.

我已经看到了在使用时如何解决这个问题NSURLConnection(例如,参见旧的iphone 3g上的HTTPS请求),但是有什么可以用UIWebView

我想我可以重新编写代码,以便它用于NSURLConnection发出请求,然后通过调用其loadHTMLString:baseURL:方法将结果放入Web视图中,但当页面包含图像,CSS,JavaScript等时,这将变得复杂.有没有更简单的方法?

Dar*_*arc 22

请注意:此API目前不受支持,实际上只能在安全的测试环境中使用.有关详细信息,请参阅此CocoaNetics文章.

[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];将允许您忽略证书错误.您还需要将以下内容添加到文件的开头,以授予您访问这些私有API的权限:

@interface NSURLRequest (DummyInterface)
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host;
+ (void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString*)host;
@end
Run Code Online (Sandbox Code Playgroud)

  • 它非常适合使用自签名的localhost证书.我用`#if DEBUG`包围了所有内容,以免被拒绝. (9认同)
  • 使用此方法将使您的App被AppStore拒绝. (4认同)

gre*_*nto 16

大家都知道...... APPLE不会接受上面隐藏接口的使用.他们寻求使用私有API,这不是一个可接受的解决方案.因此,请不要将上述解决方案作为解决方法的方式发布,因为尽管它有效,但它会在AppStore中拒绝您.这使得它毫无用处.

以下是忽略无效服务器证书的ACCEPTABLE方法.您需要使用NSURLConnection并手动加载网页数据,如下所示:

.
.
.
    //Create a URL object.
    url = [NSURL URLWithString:urlAddress];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:requestObj delegate:self];
    [connection start];
}
Run Code Online (Sandbox Code Playgroud)

然后,在你的代表....

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace 
{
    return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge 
{
    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
    {
        [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
    }
    else
    {
        [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{
[resultData appendData:data];
}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{
    NSString *htmlString = [[NSString alloc] initWithBytes:[resultData bytes] length:[resultData length] encoding:NSUTF8StringEncoding];
    [webView loadHTMLString:htmlString baseURL:url];
}
@end
Run Code Online (Sandbox Code Playgroud)

其中resultData是您之前实例化的NSMutableData,其中url和urlAddress都是您已实例化并在其他地方填充的内容.

不幸的是,我目前还不知道如何让实际的UIWebView直接加载页面而无需有效的证书.

你的,GC

  • 这种方法的问题是你的webview不会显示任何动态内容 - CSS不会加载,javascript或任何嵌入式REST调用等.这是我目前的问题.这里有一种令人费解的方式[建议](http://stackoverflow.com/questions/11573164/uiwebview-to-view-self-signed-websites-no-private-api-not-nsurlconnection-i) (2认同)

Gre*_*reg 0

据我所知,仅靠UIWebView. 据我了解,您需要使用NSURLConnection来处理所有 HTTP/HTTPS 魔力,然后将其结果提供给UIWebViewvia-loadHtmlString:baseURL:-loadData:MIMEType:textEncodingName:baseURL:.