0

I'm trying to read the file creation date and modified date from files that the user can pick from the iOS photo library and they are then copied to a temp location in the app sandbox under Documents (I copy them to Documents/temp/upload).

I then try to read the dates from the copied file with this method:

static func getCreationDateOf(_ fileURL: URL) -> Date?
{
    do
    {
        let attr = try FileManager.default.attributesOfItem(atPath: fileURL.absoluteString)
        return attr[FileAttributeKey.creationDate] as? Date
    }
    catch
    {
        print("Failed to get file creation date: \(error.localizedDescription)")
        return nil
    }
}

But the date retrieval fails with:

The file ... couldn’t be opened because there is no such file.

The files are copied over from a PHPickerViewController [PHPickerResult] results with itemProvider.loadFileRepresentation.

The error also happens if I try to read from the source file directly, without copying to the temp location. The file in the temp location is only deleted after the whole upload process is complete so it is physically there and I don't understand why iOS tells me the file is not existing. What could be the issue here?

BadmintonCat
  • 9,416
  • 14
  • 78
  • 129
  • 1
    The issue there is the url scheme. AbsoluteString is not the same as a path. check the duplicate [post](https://stackoverflow.com/questions/40642217/uiimagecontentsoffile-returning-nil-despite-file-existing-in-caches-directory). `attributesOfItem(atPath: fileURL.path)`. Btw I recommend working with the URL directly using [url resourceValues](https://stackoverflow.com/a/39501096/2303865) – Leo Dabus May 31 '22 at 06:04
  • Thanks! using `path` instead of `absolutePath` fixes the issue. – BadmintonCat May 31 '22 at 07:59

0 Answers0