Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fetching an image from a https link

Tags:

ios

https

ssl

I have just started learning ios development, and I am trying to get an image from website which uses ssl, when i connect to the site through a browser(laptop) there is a warning which says that the root certificate is not trusted, I am not the owner of the website, however I can fully trust it. My first attempt:

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

so I get this error

NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9807)

I have tried to send users to the picture link by starting ios web browser, when they do that, they would get a message asking them if they could trust it or not, if they hit yes the image will appear, however i want the image to appear inside the application.

I have also tried to use web view but it didn't work.

Most of the similar questions in here suggested using this

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

but these two methods were never called when I add them.

like image 248
B.I.A Avatar asked Jun 26 '26 01:06

B.I.A


1 Answers

Try adding these two methods

- (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];
}
like image 121
user2179059 Avatar answered Jun 28 '26 18:06

user2179059



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!