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

99封情书 提交于 2019-12-22 07:29:06

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/32770050/the-file-couldn-t-be-opened-because-url-type-http-isn-t-supported

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!