5

Using iOS 9 I'm attempting to use NSFileManager's moveItemAtURL:

do {
    print(localURL) // http://localhost:3000/api/v1/activities
    print(cacheFile) // file:///Users/kyledecot/Library/Developer/CoreSimulator/Devices/35C03988-D8F5-42E5-AB35-B99BE461EEAE/data/Containers/Data/Application/69593B3A-F764-4BC3-89AD-72B701BF85C8/Library/Caches/activities.json 
    try fileManager.moveItemAtURL(localURL, toURL: cacheFile)
} catch let error as NSError {
    print(error)
}

When catching the error I'm getting:

Error Domain=NSCocoaErrorDomain Code=262 "The file “activities” couldn’t be opened because URL type http isn’t supported." UserInfo={NSURL=http://localhost:3000/api/v1/activities}

Update #1

I've already added the appropriate values to my Info.plist to ensure that ATS is happy (see screenshot). What's odd is that I am able to download the data from my local server using HTTP (via dataTaskWithRequest:) but NSFileManager then complains about the same URL when trying to perform moveItemAtURL.

enter image description here

Kyle Decot
  • 20,715
  • 39
  • 142
  • 263
  • Matt's answer is correct. Since you are using `localhost` I assume you have a local setup of your API for testing. I would recommend disabling ATS [only in your Debug](http://stackoverflow.com/questions/32390228/is-it-possible-to-disable-ats-in-ios-9-just-for-debug-environment) configuration so that ATS is still applicable in the released application. Another approach would be to whitelist localhost explicitly rather than a blanket approach of disabling ATS all together. – vcsjones Sep 24 '15 at 20:18
  • @vcsjones I've added the required keys for (temporarily) disabling ATS but to no avail. Please see my updated question. – Kyle Decot Sep 24 '15 at 20:28
  • 1
    Well, but `localURL` is not a ` file:///` URL, so it's hard to see what on earth you expect to happen here. NSFileManager deals with _files_, not URLs in the broader sense. Your code makes no sense. Updated my answer to point that out. :) – matt Sep 24 '15 at 20:32

1 Answers1

11

There are two things to know here:

  • In iOS 9, by default, http:// is not supported. You must communicate securely (with https://). You can turn off this feature in your Info.plist if you have to.

  • NSFileManager URLs must be paths to files on disk — that is, they must be file URLs. Yours is not; it's an http:// URL. If your goal is to download a file and then copy it somewhere, use NSURLSession's download task.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • I added `NSAllowsArbitraryLoads (YES)` under `NSAppTransportSecurity` but it didn't seem to help. Any ideas? – Kyle Decot Sep 24 '15 at 20:17