xcode 无法打开文件,因为不支持 URL 类型 http

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32770050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:56:08  来源:igfitidea点击:

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

iosxcodeswiftios9

提问by Kyle Decot

Using iOS 9 I'm attempting to use NSFileManager'smoveItemAtURL:

使用 iOS 9 我试图使用NSFileManager'smoveItemAtURL

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}

错误域=NSCocoaErrorDomain 代码=262“无法打开文件“活动”,因为不支持 URL 类型 http。UserInfo={NSURL= http://localhost:3000/api/v1/activities}

Update #1

更新 #1

I've already added the appropriate values to my Info.plistto 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.

我已经为 my 添加了适当的值以Info.plist确保 ATS 满意(请参见屏幕截图)。奇怪的是,我能够使用 HTTP(通过dataTaskWithRequest:)从本地服务器下载数据,但是 NSFileManager 在尝试执行moveItemAtURL.

enter image description here

在此处输入图片说明

回答by matt

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.plistif you have to.

  • NSFileManager URLs must be paths to files on disk — that is, they must be fileURLs. 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.

  • 在 iOS 9 中,默认情况下http://不支持。您必须安全地(与https://)通信。如果需要,您可以在Info.plist 中关闭此功能。

  • NSFileManager URL 必须是磁盘上文件的路径——也就是说,它们必须是文件URL。你的不是;这是一个http://网址。如果您的目标是下载文件然后将其复制到某处,请使用 NSURLSession 的下载任务。