0

I'm trying to download simple image from url http://d1vqbpto5tbbz0.cloudfront.net/blog/wp-content/uploads/2014/08/25215844/ios-logo1.png

This is simple url access through browser without any login.

I have tried with this code:

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
configuration.timeoutIntervalForRequest = timeout;
NSURLSession *urlSession = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
[[urlSession dataTaskWithURL:urlImage] resume];

For this only I am receiving authentication challenge.

What is the reason for it?

I have gone through stackoverflow and found these links:

In all links, there is mentioned to enter user:password. What user name and password, I can enter?

Himanth
  • 2,381
  • 3
  • 28
  • 41
PK86
  • 1,218
  • 2
  • 14
  • 25
  • It could be App Transport Security issue? – GeneCode Jul 26 '17 at 06:39
  • I have added NSAppTransportSecurity NSAllowsArbitraryLoads – PK86 Jul 26 '17 at 06:41
  • The method `URLSession:task:didReceiveChallenge:completionHandler:` is useful when you need session with authorization or for implementing certificate pinning. In 99% cases it's not necessary, you can safely remove it, because if it is implemented, it will be called. – mag_zbc Jul 26 '17 at 06:44
  • If the url can be accessed by browser without auth, then iOS should be able to access it too. I think there is some other problem unrelated to NSURLSession. – GeneCode Jul 26 '17 at 06:44

2 Answers2

2

You have implement the NSURLSessionDelegate protocol method didReceiveChallenge.

Whenever authentication challenge occurs this method will get calls. And you have to mention it is a trusted server.

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
 completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition
                             disposition, NSURLCredential *credential))completionHandler
{
    completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
}
Subramanian P
  • 4,365
  • 2
  • 21
  • 25
0

Looks the issue is most likely due to the fact that the site isn't using SSL/TSA. To handle the challenge and load the image conform to the NSURLSessionDelegate delegate and implement the following method.

- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler;

EDIT

Another option would be to specify arbitrary loads in your plist to allow loading of HTTP links. It's not recommended for a released app but for testing it's more then fine.

Add the following to your plist:

<key>NSAppTransportSecurity</key>
<dict>
  <!--Include to allow all connections (DANGER)-->
  <key>NSAllowsArbitraryLoads</key>
      <true/>
</dict>
Harry Singh
  • 826
  • 5
  • 11