从https链接获取图片

我刚开始学习ios开发,我试图从网站上获得一个使用SSL的图像,当我通过浏览器(笔记本电脑)连接到网站有一个警告,说根证书不受信任,我是不是网站的所有者,但我可以完全信任它。 我第一次尝试:

self.eventImage.image = [UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString:imageUrl]]]; 

所以我得到这个错误

NSURLConnection / CFURLConnection HTTP加载失败(kCFStreamErrorDomainSSL,-9807)

我试图通过启动ios web浏览器发送用户到图片链接,当他们这样做时,他们会得到一个消息,询问他们是否可以信任它,如果他们打到是图像会出现,但是我想要的图像出现在应用程序内部。

我也试图使用networking视图,但没有奏效。

这里的大部分类似的问题都build议使用这个

 - (BOOL)connection:(NSURLConnection *) connection canAuthenticateAgainstProtectionSpace: (NSURLProtectionSpace *)protectionSpace { return NO; //return [protectionSpace.authenticationMethod isEqualToString: // NSURLAuthenticationMethodServerTrust]; } - (void)connection:(NSURLConnection *) connection didReceiveAuthenticationChallenge: (NSURLAuthenticationChallenge *)challenge { NSString *imageUri =[self.detailItem objectForKey: @"image"]; NSArray *trustedHosts = [[NSArray alloc]initWithObjects:imageUri, nil]; 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]; } 

但是当我添加它们时,这两种方法从来没有被调用过。

改变你的代码。 而不是使用NSData dataWithContentsOfURL:你需要使用你自己的显式NSURLConnection 。 然后你可以使用适当的NSURLConnectionDelegate方法。

另一个select是使用stream行的AFNetworking库 。

尝试添加这两个方法

 - (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace { return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]; } -(void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge]; [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge]; } 

rmaddy,user2179059和Anindya Sengupta的回答有助于解决这个问题。

首先,我明确地使用了NSURLConnection ,并且为了安全连接,我使用了这种方法(从这个博客文章中获得 )

 -(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]) { // instead of XXX.XXX.XXX, add the host URL, // if this didn't work, print out the error you receive. if ([challenge.protectionSpace.host isEqualToString:@"XXX.XXX.XXX"]) { NSLog(@"Allowing bypass..."); NSURLCredential *credential = [NSURLCredential credentialForTrust: challenge.protectionSpace.serverTrust]; [challenge.sender useCredential:credential forAuthenticationChallenge:challenge]; } } [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge]; } 

这与user2179059的不同之处在于将不安全的连接限制在该主机上。

我希望你在.h文件的界面中包含NSURLConnectionDelegate协议?

 @interface ConnectionExampleViewController : UIViewController <NSURLConnectionDelegate>