1

I am trying to show a list of files from my computer using a server app that uses the IP address and you can look for a file from the phone.

I am fetching files successfully, but I have a problem with showing the size and creation date of files. This works fine on the iOS simulator!!! but on the real device, I am getting this error for each file:

FileAttribute error: Error Domain=NSCocoaErrorDomain Code=260 "The file “office-decor.jpg” couldn’t be opened because there is no such file." UserInfo={NSFilePath=/Users/username/Desktop/My%20office%20stuff/office-decor.jpg, NSUnderlyingError=0x281e279c0 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}

I have created an URL extension to show date, size:

extension URL {
    var attributes: [FileAttributeKey : Any]? {
        do {
            return try FileManager.default.attributesOfItem(atPath: path)
        } catch let error as NSError {
            print("FileAttribute error: \(error)")
        }
        return nil
    }

    var fileSize: UInt64 {
        return attributes?[.size] as? UInt64 ?? UInt64(0)
    }

    var fileSizeString: String {
        return ByteCountFormatter.string(fromByteCount: Int64(fileSize), countStyle: .file)
    }

    var creationDate: Date? {
        return attributes?[.creationDate] as? Date
    }
}

my file path is something like this:

Users/username/Desktop/xxx.jpg

and trying to get the file path using URL like this to get the file size:

private var fileSize: String {
        guard let url = URL(string: path.addURLEncoding()) else { return "-" }
        return url.fileSizeString
    }

extension String {
    func addURLEncoding() -> String {
        return self.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
    }
}

I also tried file:// and URL(filePath...), URL(fileURLWithPath but still get that error on the device, which is working totally fine on simulator. Any idea how to fix that?

Mc.Lover
  • 4,813
  • 9
  • 46
  • 80
  • I'm not sure that adding percent escape is a good solution. Could you show how you get the initial URL? Or Path? – Larme Jan 09 '23 at 12:16
  • @Larme I am receiving a JSON contains array of `String` of paths like this `["/Users/username/Desktop/My office stuff/new office logo.png",]` – Mc.Lover Jan 09 '23 at 13:02
  • Then you should construct `URL(fileURLWithPath: thePath)`. Have you the rights to see your desktop files? – Larme Jan 09 '23 at 14:49
  • @Larme I mentioned in my Q that I tested `URL(filePath...)` but no success! – Mc.Lover Jan 09 '23 at 14:57
  • 1
    Adding percent encoding explicitly for file system paths is **wrong**, `URL(fileURLWithPath` does the job. And I would prefer `resourceValues(forKeys` of `URL` over *detour* `attributesOfItem(atPath` of `FileManager` – vadian Jan 11 '23 at 13:44
  • As @vadian pointed out correctly, do *not* encode file paths. ```URL(fileURLWithPath```creates the correct URL. First check the URL that you are getting. Then check if you can access it (depending on OS version and process the location might not be available without asking the users for it), – DatBlaueHus Jan 11 '23 at 15:58
  • It works in the simulator because the simulator is running on your Mac and it has access to your home directory on your Mac. Your iOS device doesn't contain those files so it reports them as not found. Remember, a file URL is only valid on the local device. – HangarRash Jan 12 '23 at 02:37
  • @vadian I've tried it out! but the same result! – Mc.Lover Jan 12 '23 at 08:39

0 Answers0