Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The file couldn’t be opened because URL type http isn’t supported

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

like image 540
Kyle Decot Avatar asked Sep 24 '15 20:09

Kyle Decot


1 Answers

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.

like image 69
matt Avatar answered Nov 06 '22 08:11

matt