Swift 3.0 FileManager.fileExists(atPath:) always return false Swift 3.0 FileManager.fileExists(atPath:) always return false ios ios

Swift 3.0 FileManager.fileExists(atPath:) always return false


I assume your url is an URL type. If so try this out:

let filePath = url?.path  // always try to work with URL when accessing Filesif(FileManager.default.fileExists(atPath: filePath!)){  // just use String when you have to check for existence of your file    let result = NSData(contentsOf: url!)  // use URL instead of String}

Saying enough, you should change your implementation like this:

if(FileManager.default.fileExists(atPath: (url?.path)!)){  // just use String when you have to check for existence of your file    let result = NSData(contentsOf: url!)  // use URL instead of String}

EDIT: 1

There is even more better way, you can call it swift-way (:D). You don't have to explicitly check for file existence.

guard let result = NSData(contentsOf: fileURL) else {    // No data in your fileURL. So no data is received. Do your task if you got no data    // Keep in mind that you don't have access to your result here.    // You can return from here.     return}// You got your data successfully that was in your fileURL location. Do your task with your result.// You can have access to your result variable here. You can do further with result constant.print(result)

Update for Swift 3.0+ without the Objective-Cish NS prefix:

do {    let result = try Data(contentsOf: fileURL)    print(result)} catch {    print(error)}


in swift 3just in case anyone gets confused like i did, here's the full snippets:

let str = "file:///Users/martian2049/Library/Developer/CoreSimulator/Devices/67D744AA-6EEC-4AFD-A840-366F4D78A18C/data/Containers/Data/Application/DD96F423-AF9F-4F4D-B370-94ADE7D6D0A5/Documents/72b8b0fb-7f71-7f31-ac9b-f9cc95dfe90d.mp3"let url = URL(string: str)print(url!.path,"\n")if FileManager.default.fileExists(atPath: url!.path) {    print("FILE Yes AVAILABLE")} else {    print("FILE NOT AVAILABLE")}

this prints

/Users/martian2049/Library/Developer/CoreSimulator/Devices/67D744AA-6EEC-4AFD-A840-366F4D78A18C/data/Containers/Data/Application/DD96F423-AF9F-4F4D-B370-94ADE7D6D0A5/Documents/72b8b0fb-7f71-7f31-ac9b-f9cc95dfe90d.mp3 FILE Yes AVAILABLE

notice how the 'file://' got chopped off?


I want to share my experience, in case anyone else gets baffled by this.

Tested on iOS 10-11, Xcode 9.2 and Swift 3.2.

Short answer: if you save a file path to disk, you may solve by not including the Documents directory in it.Instead, every time you need to retrieve the file with the saved path, get the Documents directory and append the path.


For an iOS app, I was saving an image to .../Documents/Pictures through the relative URL, let's say url.As the image was saved, a path, let's say url.path, was saved too in a Core Data entity.

When I later tried retrieving the image through FileManager.default.fileExists(atPath: url.path), it always returned false.

I was testing the app on my iPhone. It turned out that, for some reason, every time I ran the app from Xcode, the app identifier folder changed!!

So:

  • App opened from Xcode -> Image saved -> app closed -> app opened from physical device ->fileExists -> TRUE
  • App opened from Xcode -> Image saved -> app closed -> app opened from Xcode -> fileExists -> FALSE

You can check if this is your case by getting and printing the Document folder path (or URL, it doesn't matter) and comparing it with the saved path (or URL). If you get something like this:

  • /var/mobile/Containers/Data/Application/5D4632AE-C432-4D37-A3F7-ECD05716AD8A/Documents..
  • /var/mobile/Containers/Data/Application/D09904C3-D80D-48EB-ACFB-1E42D878AFA4/Documents..

you found the issue.