File cannot be opened because http url is not supported

Using iOS 9 I'm trying 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 I get an error, I get:

Domain Error = NSCocoaErrorDomain Code = 262 "The file" action "could not be opened because the http URL is not supported." UserInfo = {NSURL = http: // localhost: 3000 / api / v1 / activities }

Update # 1

I have already added the appropriate values ​​to my Info.plist to make sure that the ATS is happy (see screenshot). Which is strange that I can download data from my local server using HTTP (via dataTaskWithRequest: , but NSFileManager then complains about the same URL when trying to execute moveItemAtURL .

enter image description here

+5
source share
1 answer

There are two things here:

  • In iOS 9, http:// not supported by default. You must communicate securely (using https:// ). You can disable this feature in your Info.plist if you need to.

  • NSFileManager URLs must be paths to files on disk, that is, they must be file URLs. Your not; this is the URL http:// . If your goal is to download the file and then copy it somewhere, use the NSURLSession download task.

+10
source

Source: https://habr.com/ru/post/1232213/


All Articles