Jon
Jon

Reputation: 687

CFURLCopyResourcePropertyForKey failed because passed URL no scheme

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

Upvotes: 56

Views: 32760

Answers (7)

Mike Casan Ballester
Mike Casan Ballester

Reputation: 1770

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

Upvotes: 9

iNoob
iNoob

Reputation: 142

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)

}

Upvotes: 0

zavidovych
zavidovych

Reputation: 4868

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

Upvotes: -2

Renato Ioshida
Renato Ioshida

Reputation: 431

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)!

Upvotes: 15

huangqibiao
huangqibiao

Reputation: 91

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

Upvotes: 7

Jorge Irún
Jorge Irún

Reputation: 1753

For me this problem was fixed by adding

file://

right before the file path address like this:

var filePath = "file://\(fileURLpath)"

Upvotes: 109

leonard
leonard

Reputation: 2387

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

Upvotes: 25

Related Questions