56

I have a program that saves a file to the iCloud and this has worked great for iOS7, but now I get this error with iOS8 and cannot seem to find the answer on how to fix it. Anyone else had this problem? Any ideas would be greatly appreciated.

The Error: CFURLCopyResourcePropertyForKey failed because it was passed this URL which has no scheme: /var/mobile/Containers/Data/Application/ASFHDDE3-B1BB-41D7-A47C-DCC328362W21/Documents/mypictostore.png

The Line of Code Throws Error: [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

Thank you, Jon

Jon
  • 687
  • 1
  • 9
  • 14
  • 9
    I fixed this by appending file:/// to the front of the url string. I am posting the solution so others may find it if they are having the same problem. – Jon Sep 28 '14 at 00:03
  • 7
    Try using [NSURL fileURLWithPath:]; instead – cmlloyd Mar 20 '15 at 16:32
  • I had a similar issue to @Jon I was pulling images from gstatic.com which has no scheme. I solved it by adding an "http://" to the front of the url string. – UncaughtException Mar 01 '16 at 05:50

7 Answers7

108

For me this problem was fixed by adding

file://

right before the file path address like this:

var filePath = "file://\(fileURLpath)"
Jorge Irún
  • 1,683
  • 1
  • 15
  • 13
23

Or maybe you can use NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("mypictostore", ofType: "png")!) instead of using NSURL(string: NSBundle.mainBundle().pathForResource("mypictostore", ofType: "png")!)

leonard
  • 2,337
  • 4
  • 22
  • 26
15

Look this link for Objective-c answer: CFURLSetResourcePropertyForKey failed when disable data backup on NSDocumentDirectory

Older Swift version answer:

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 Answer:

var str:String = "/var/mobile/Containers/Data/Application/3039975A-5E05-4A4C-8000-55C681A7C35F/Documents/index.html"

var url:URL = URL(string: str)!
Renato Ioshida
  • 421
  • 3
  • 6
9

Depending on what you need to do with the path, you may need to prepend the scheme file://. See more at documentation/foundation/url

If you need the file path, use fileURLWithPath:

let imageURL = URL(fileURLWithPath: imagePath)

it will give you the path as

file:///var/mobile/Containers/Data/Application/7C1D854B-8A2E-4FF0-BD30-0652AEE10B6F/Documents/image_8ZMAM.jpg


If you need the path without the scheme, use string:

let imageURL = URL(string: imagePath)

it will give you the path as

/var/mobile/Containers/Data/Application/7C1D854B-8A2E-4FF0-BD30-0652AEE10B6F/Documents/image_8ZMAM.jpg

Mike Casan Ballester
  • 1,690
  • 19
  • 33
7

look, I find this,The url is The location to write the data into. we should tell the system a file path.

  • var filePath = "file://(fileURLpath)"
  • var url:URL = URL.init(fileURLWithPath: fileURLpath)

Data

huangqibiao
  • 91
  • 1
  • 3
0

You can also try this, I think the following code is great as if you are not sure any particular url is correct or not for the CFURLCopyResourcePropertyForKey

if testURL.isFileURL == false {

    testURL = URL(fileURLWithPath: testURL.absoluteString)

}
iNoob
  • 144
  • 1
  • 15
-2

var url = USL(fileUrlWithString: "/private/var/...")

zavidovych
  • 4,718
  • 2
  • 24
  • 22
  • 1
    Please do not post only code as an answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually of higher quality, and are more likely to attract upvotes. – Boken Apr 20 '20 at 01:08