iphone:安全restfull服务器“此服务器的证书无效

我正在尝试使用安全的宁静的服务,这给了错误

错误=错误域= NSURLErrorDomain代码= -1202“此服务器的证书无效,您可能正在连接假装为”xxx.xxx.xxx.xxx“的服务器,这可能会将您的机密信息置于危险之中。

在xCode 4.2上工作,在哪里错误或缺less任何步骤。

使用下面的代码

RegisterUser.f

@interface RegisterUser : UIViewController<UITextFieldDelegate, UIScrollViewDelegate, NSURLConnectionDelegate> 

RegisterUser.m

 - (IBAction)SubmitBtnAction:(id)sender { NSURL *url = [NSURL URLWithString:@"https://xx.xx.xx.xxx:8223/jaxrs/tunedoorgateway/getCountries"]; NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url]; [NSURLConnection sendAsynchronousRequest:urlRequest queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { if ([data length] >0 && error == nil) { NSLog(@"Data = %@", data); // DO YOUR WORK HERE } else if ([data length] == 0 && error == nil) { NSLog(@"Nothing was downloaded."); } else if (error != nil){ NSLog(@"Error = %@", error); } }]; } - (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace { NSLog(@"This is canAuthenticateAgainstProtectionSpace"); return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]; } - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { // if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) // if ([trustedHosts containsObject:challenge.protectionSpace.host]) [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge]; [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge]; NSLog(@"This is didReceiveAuthenticationChallenge"); // [[challenge sender] cancelAuthenticationChallenge:challenge]; } 

我觉得这可能是因为DNS。 就像你的服务器没有注册或一些。

尝试将其用于开发的目的 –

编辑:

创build一个“NSURLRequest + NSURLRequestSSLY.h”文件并添加这些行

 #import <Foundation/Foundation.h> @interface NSURLRequest (NSURLRequestSSLY) +(BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host; @end 

创build一个“NSURLRequest + NSURLRequestSSLY.m”文件并添加这些行

 #import "NSURLRequest+NSURLRequestSSLY.h" @implementation NSURLRequest (NSURLRequestSSLY) +(BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host { return YES; } @end 

不要忘记删除它,因为你的应用程序可能会被拒绝。

失败不在您的代码中。 您正在使用不提供已知证书的HTTPS服务器。 如果你自己安装了服务器,你必须从iOS和大多数其他操作系统所信任的大型authentication机构那里购买一张单据证书。

出于开发目的,您可以通过忽略不可信证书来testing您的REST服务。 按照这个指导做到这一点: http : //www.cocoanetics.com/2009/11/ignoring-certificate-errors-on-nsurlrequest/

但是对于生产用途,我build议你不要使用这个方法,因为它会给你的应用程序带来安全漏洞。 如果您忽视安全性,则也可以使用HTTP而不是HTTPS。

我已经尝试了几种方法,发现它与苹果开发者中心证书有关。 升级您的configuration,然后重试。 我曾尝试发布iPad,iPhone 5和5S,但iPhone 4的数据。当我更新我的configuration和安装在我的iPhone 4,现在的问题。