Error Copying Files with Filemanager (Cfurlcopyresourcepropertyforkey Failed Because It Was Passed an Url Which Has No Scheme)

Error copying files with FileManager (CFURLCopyResourcePropertyForKey failed because it was passed an URL which has no scheme)

The error occurs because

URL(string: "/Users/xxx/Desktop/Media/")!

creates a URL without a scheme. You can use

URL(string: "file:///Users/xxx/Desktop/Media/")!

or, more simply,

URL(fileURLWithPath: "/Users/xxx/Desktop/Media/")

Note also that in fileManager.copyItem() the destination must
include the file name, and not only the destination
directory:

try fileManager.copyItem(at: fileURL,
to: location.appendingPathComponent(fileURL.lastPathComponent))

CFURLCopyResourcePropertyForKey failed because passed URL no scheme

For me this problem was fixed by adding

file://

right before the file path address like this:

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

Swift 3: Error in copying file with FileManager

File system URLs must be created with the fileURLWithPath initializer which adds the file:// scheme the error message complains about:

let fromURL = URL(fileURLWithPath: bundlePath)
let toURL = URL(fileURLWithPath: path)

Nevertheless there is a more convenient way to create fromURL:

if let fromURL = Bundle.main.url(forResource: "Data", withExtension: "plist") { ...

I recommend to use generally the URL related API, for example

var docDirectory: URL {
return try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
}

var dataFileURL: URL {
return docDirectory.appendingPathComponent("Data.plist")
}

The huge benefit is you get non-optional values and get rid of a couple of guards.

CFURLSetResourcePropertyForKey failed when disable data backup on NSDocumentDirectory

Solved. once I changed

NSURL *fileURL = [NSURL URLWithString:localFilePath];

to

NSURL *fileURL = [NSURL fileURLWithPath:localFilePath];

everything work perfectly.

Swift: failing to copy files to a newly created folder

From the copyItemAtPath(...) documentation:

dstPath
The path at which to place the copy of srcPath. This path must
include the name of the file or directory in its new location. ...

You have to append the file name to the destination directory for the
copyItemAtPath() call (code updated for Swift 3 and later)

let srcURL = URL(fileURLWithPath: fullElementPath)
let destURL = URL(fileURLWithPath: newMTSFolder).appendingPathComponent(srcURL.lastPathComponent)

do {
try FileManager.default.copyItem(at: srcURL, to: destURL)
} catch {
print("copy failed:", error.localizedDescription)
}

How do I save an image to a png file?

First of all never use synchronous Data(contentsOf to load data from a server.

The error occurs because the URL has the wrong format

  • A remote URL must contain a scheme and host (http://server.com...)
  • An local URL in the file system must be created with URL(fileURLWithPath: and the path must start with a slash.


Related Topics



Leave a reply



Submit