1

I have super simple code that downloads an image from the web. It was working fine in iOS 8 but now I keep getting this error now in Xcode 7.3 with iOS 9. Is this a bug?

NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask ,YES);
NSString* documentsPath = [paths objectAtIndex:0];

NSError *error;
NSString *data2URL = @"http://cdn.minecraftpocket-servers.com/images/flags/Brazil.png";
NSURL *url = [NSURL URLWithString:data2URL];
NSString *imgFileNameStr = [data2URL lastPathComponent];

NSData *data2 = [NSData dataWithContentsOfURL:url options: NSDataReadingUncached error: &error];

if (error)
    NSLog(@"Download error: %@",error);

//check
if (data2 == NULL)
{
    NSLog(@"DATA IS NULL");
}
else
{
    NSLog(@"DATA IS NOT NULL");
}

//saving file
NSString* fullPathToFile2 = [documentsPath stringByAppendingPathComponent:imgFileNameStr];
BOOL success = [data2 writeToFile:fullPathToFile2 atomically:NO];
NSLog(@"Success = %d ...", success);

ERROR:

2016-07-03 23:00:36.963 014-test-proj[15404:419151] Download error: Error Domain=NSCocoaErrorDomain Code=256 "The file “Brazil.png” couldn’t be opened." UserInfo={NSURL=http://cdn.minecraftpocket-servers.com/images/flags/Brazil.png}
2016-07-03 23:00:36.963 014-test-proj[15404:419151] DATA IS NULL
2016-07-03 23:00:36.964 014-test-proj[15404:419151] Success = 0 ...

I have already checked & made sure of my plist file for application security.

enter image description here

Someone had the same problem HERE. What did I miss?

UPDATE: Code works if I use this link ...

NSString *data2URL =@"https://upload.wikimedia.org/wikipedia/commons/1/1e/Large_Siamese_cat_tosses_a_mouse.jpg";

I don't get it. Why would it have an issue with the other one.

Community
  • 1
  • 1
Sam B
  • 27,273
  • 15
  • 84
  • 121

2 Answers2

1

You should never have been calling [NSData dataWithContentsOfURL:url] to download data from a remote URL (i.e. somewhere across the Internet). Your code was skanky, and you were doing synchronous networking, which is always wrong. Now you're being stopped by the runtime, and rightly so. To download data, download it. Use NSURLSession. That's what it's for.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • nope, that has no effect. I changed the code to NSURLSession, it downloads the file but it cannot be opened. There is something else going on with it. Try it for yourself. PS. See updated link – Sam B Jul 04 '16 at 03:31
  • I notice there's a redirect. That probably has something to do with it. – matt Jul 04 '16 at 03:42
-1

The difference is the change to using a secure URL. Apple has introduced App Transport Security (ATS), and there is now a setting in your application plist that you have to enable in order to be able to load from URL that is not of the http so form.

Add the following to your Info.plist to disable ATS

<key>NSAppTransportSecurity</key>  
     <dict>  
          <key>NSAllowsArbitraryLoads</key><true/>  
     </dict>  
Feldur
  • 1,121
  • 9
  • 23