问题描述
我收到此错误
The certificate for this server is invalid. You might be connecting to a server
that is pretending to be "server addres goes here" which could put your
confidential information at risk."
我使用这种方法:
[NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];
我可以解决这个问题吗?
How can I fix this?
我试过这个代码:
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request
delegate:self];
那么我在didReceiveResponse方法中获取EXC_BAD_ACCESS。
but then I get EXC_BAD_ACCESS in the didReceiveResponse method.
推荐答案
如果不发送任何敏感信息,您可以忽略无效的证书。 介绍了您可以如何做那。以下是对所述方法之一的示例实现
You could simply ignore the invalid certificate if you are not sending any sensitive information. This article describes how you could do that. Here is an example implementation by Alexandre Colucci for one of the methods described in that article.
实质上,您要在 @implementation
上方定义一个虚拟接口:
Essentially you want to define a dummy interface just above the @implementation
:
@interface NSURLRequest (DummyInterface)
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host;
+ (void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString*)host;
@end
然后在调用 sendSynchronousRequest
,调用在虚拟接口中定义的私有方法:
And before you call sendSynchronousRequest
, invoke the private method you defined in the dummy interface:
[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[URL host]];
这篇关于使用NSURLConnection忽略证书错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!