简体   繁体   English

如何在UIWebView上加载网页(使用自签名证书的Https)?

[英]How to load a webpage (Https using a self signed certificate) on the UIWebView?

How to load a webpage (Https using a self signed certificate) on the UIWebView (iOS)? 如何在UIWebView(iOS)上加载网页(使用自签名证书的Https)? I tried using NSURLConnection and I am able to load the NSMutableData to the UIwebView. 我尝试使用NSURLConnection,并且能够将NSMutableData加载到UIwebView。 But in this case I am not able to see the images in that page. 但是在这种情况下,我无法看到该页面中的图像。

[yourWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"yourUrlGoesHere"]]];

And about the certificates part if your URL openable in Safari it will open in a webview. 关于证书部分,如果您的URL可在Safari中打开,它将在Web视图中打开。


Update for the comment 更新评论

Try the below 试试下面

NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"yourUrlGoesHere"]];
NSURLConnection *urlConnection=[NSURLConnection connectionWithRequest:req delegate:self];
[yourWebView loadRequest:req];

and implement the following delegate methods in your class, 并在您的课程中实现以下委托方法,

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

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {

    [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}

Self signed certificates most of the time can not be validated by client, thats why the client can not load the data, because it can not validate from server. 客户端大部分时间无法验证自签名证书,这就是客户端无法加载数据的原因,因为它无法从服务器进行验证。

Certificates uses domains to validate itself so it is a bit hard to have on development environment. 证书使用域进行自我验证,因此在开发环境中有点困难。

There is a tool which creates for local environment certificate, i am not sure how it is internally working my best thought is it adds another CA file on your environment. 有一个为本地环境证书创建的工具,我不确定它在内部如何工作,我最好是在您的环境中添加另一个CA文件。

You may want to check mkcert it is very easy to use nice workaround. 您可能要检查mkcert ,使用好解决方法非常容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM