从https链接获取图像

时间:2013-11-12 23:14:49

标签: ios ssl https

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

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

所以我收到了这个错误

  

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

我试图通过启动ios网络浏览器将用户发送到图片链接,当他们这样做时,他们会收到一条消息,询问他们是否可以信任,如果他们点击是,则图像会出现,但是我希望图像出现在应用程序中。

我也尝试使用网络视图,但它没有用。

此处的大多数类似问题建议使用此

- (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];
}

但是我添加它们时从未调用过这两种方法。

4 个答案:

答案 0 :(得分:2)

尝试添加这两种方法

- (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];
}

答案 1 :(得分:1)

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

另一种选择是使用流行的AFNetworking library

答案 2 :(得分:1)

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

首先,我明确地使用了NSURLConnection,对于安全连接,我使用了这种方法(从 blog post得到它)

-(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的区别仅在于限制与该主机的不安全连接。

答案 3 :(得分:0)

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

@interface ConnectionExampleViewController : UIViewController <NSURLConnectionDelegate>