我有一个程序可以将文件保存到iCloud,这对iOS7很有用,但是现在我在iOS8上遇到这个错误,似乎无法找到解决方法。还有其他人有这个问题吗?任何想法都将不胜感激。
错误: CFURLCopyResourcePropertyForKey失败,因为它传递了这个没有方案的URL:/var/mobile/Containers/Data/Application/ASFHDDE3-B1BB-41D7-A47C-DCC328362W21/Documents/mypictostore.png
代码行引发错误: [fileManager setUbiquitous:YES itemAtURL:backupUrl destinationURL:[[ubiq URLByAppendingPathComponent:@" Documents" isDirectory:true] URLByAppendingPathComponent:backupName] error:& theError];
URLS: destinationURL:file:/// private / var / mobile / Library / Mobile%20Documents / ABC23455~MY-Program / backupUrl:/var/mobile/Containers/Data/Application/ASDFGEEW-B1BB-6FR6-A47C-DCCF21876D36/Documents/mypic.png
谢谢你, 乔恩
答案 0 :(得分:62)
对我来说,通过添加
解决了这个问题file://
就在文件路径地址之前,如下所示:
var filePath = "file://\(fileURLpath)"
答案 1 :(得分:15)
或者您可以使用NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("mypictostore", ofType: "png")!)
代替NSURL(string: NSBundle.mainBundle().pathForResource("mypictostore", ofType: "png")!)
答案 2 :(得分:9)
查看Objective-c答案的此链接:CFURLSetResourcePropertyForKey failed when disable data backup on NSDocumentDirectory
较旧的Swift版本答案:
var str:String = "/var/mobile/Containers/Data/Application/3039975A-5E05-4A4C-8000-55C681A7C35F/Documents/index.html"
var url:URL = URL.init(fileURLWithPath: str)
Swift 4答案:
var str:String = "/var/mobile/Containers/Data/Application/3039975A-5E05-4A4C-8000-55C681A7C35F/Documents/index.html"
var url:URL = URL(string: str)!
答案 3 :(得分:4)
看,我发现,url
是The location to write the data into
。我们应该告诉系统file path
。
答案 4 :(得分:0)
您也可以尝试这样做,我认为以下代码非常有用,好像您不确定CFURLCopyResourcePropertyForKey的任何特定URL是否正确
if testURL.isFileURL == false {
testURL = URL(fileURLWithPath: testURL.absoluteString)
}
答案 5 :(得分:0)
如果需要文件路径,请使用 fileURLWithPath :
let imageURL = URL(fileURLWithPath: imagePath)
它将为您提供路径
文件:///var/mobile/Containers/Data/Application/7C1D854B-8A2E-4FF0-BD30-0652AEE10B6F/Documents/image_8ZMAM.jpg
如果不需要该方案的路径,请使用 string :
let imageURL = URL(string: imagePath)
它将为您提供路径
/var/mobile/Containers/Data/Application/7C1D854B-8A2E-4FF0-BD30-0652AEE10B6F/Documents/image_8ZMAM.jpg
答案 6 :(得分:-1)
var url = USL(fileUrlWithString: "/private/var/...")