How do I move/copy a file from cache to the user's documents directory? [Swift, Flutter]

1k Views Asked by At

I have downloaded a file in Flutter and using the path_provider package, placed it in the directory I get from getTemporaryDirectory(). Then calling a method using MethodChannels, I am calling the following method:

private func saveDocument(_ filePath: String, _ result: @escaping FlutterResult) {
        let fileManager = FileManager.default
        print("File exists: \(fileManager.fileExists(atPath: filePath))") // Prints -> File exists: true
        do {
            let documentsDir = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
            let documentsDirString = documentsDir.absoluteString
            
            print("Documents dir exists: \(fileManager.fileExists(atPath: documentsDirString))") // Prints -> Documents dir exists: false
            
            if (!fileManager.fileExists(atPath: documentsDirString)) {
                try fileManager.createDirectory(at: documentsDir, withIntermediateDirectories: true, attributes: nil)
            }
            
            let fileName = (filePath as NSString).lastPathComponent
            let newFilePath = "\(documentsDir.appendingPathComponent(fileName))"
            try fileManager.copyItem(at: URL(fileURLWithPath: filePath), to: URL(fileURLWithPath: newFilePath))
            result(true)
        } catch {
            print(error)
            result(false)
        }
    }

I get the following error in the console:

File path: /Users/user/Library/Developer/CoreSimulator/Devices/A55EF2F7-FD8A-469C-A835-1A6282066F50/data/Containers/Data/Application/CE787CF1-1A9E-44C0-90FA-48928A3CE5F9/Library/Caches/filename.pdf
File exists: true
Documents dir exists: false
2021-04-26 10:01:51.693311+0530 Runner[9709:78939] open on /file:/Users/user/Library/Developer/CoreSimulator/Devices/A55EF2F7-FD8A-469C-A835-1A6282066F50/data/Containers/Data/Application/CE787CF1-1A9E-44C0-90FA-48928A3CE5F9/Documents/filename.pdf: No such file or directory
Error Domain=NSCocoaErrorDomain Code=4 "The file “filename.pdf” doesn’t exist." UserInfo={NSSourceFilePathErrorKey=/Users/user/Library/Developer/CoreSimulator/Devices/A55EF2F7-FD8A-469C-A835-1A6282066F50/data/Containers/Data/Application/CE787CF1-1A9E-44C0-90FA-48928A3CE5F9/Library/Caches/filename.pdf, NSUserStringVariant=(
    Copy
), NSDestinationFilePath=/file:/Users/user/Library/Developer/CoreSimulator/Devices/A55EF2F7-FD8A-469C-A835-1A6282066F50/data/Containers/Data/Application/CE787CF1-1A9E-44C0-90FA-48928A3CE5F9/Documents/filename.pdf, NSFilePath=/Users/user/Library/Developer/CoreSimulator/Devices/A55EF2F7-FD8A-469C-A835-1A6282066F50/data/Containers/Data/Application/CE787CF1-1A9E-44C0-90FA-48928A3CE5F9/Library/Caches/filename.pdf, NSUnderlyingError=0x6000012baa30 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}

I have read the docs and searched online for about 2 days now and I cannot find a solution.

What I am trying to achieve

Move the file into the user's file manager so that they can view, modify, share, etc. the document from the Files app

I have tried running this on a physical device as well with no luck

What I have tried:

  • Removing try fileManager.createDirectory() if statement. The following is the logs on removing this:
File path: /Users/user/Library/Developer/CoreSimulator/Devices/A55EF2F7-FD8A-469C-A835-1A6282066F50/data/Containers/Data/Application/A00C3B10-8E12-44F1-B8F8-B53692CD4B56/Library/Caches/filename.pdf
File exists: true
Documents dir exists: false
2021-04-26 10:00:11.558244+0530 Runner[9338:76668] open on /file:/Users/user/Library/Developer/CoreSimulator/Devices/A55EF2F7-FD8A-469C-A835-1A6282066F50/data/Containers/Data/Application/A00C3B10-8E12-44F1-B8F8-B53692CD4B56/Documents/filename.pdf: No such file or directory
Error Domain=NSCocoaErrorDomain Code=4 "The file “filename.pdf” doesn’t exist." UserInfo={NSSourceFilePathErrorKey=/Users/user/Library/Developer/CoreSimulator/Devices/A55EF2F7-FD8A-469C-A835-1A6282066F50/data/Containers/Data/Application/A00C3B10-8E12-44F1-B8F8-B53692CD4B56/Library/Caches/filename.pdf, NSUserStringVariant=(
    Copy
), NSDestinationFilePath=/file:/Users/user/Library/Developer/CoreSimulator/Devices/A55EF2F7-FD8A-469C-A835-1A6282066F50/data/Containers/Data/Application/A00C3B10-8E12-44F1-B8F8-B53692CD4B56/Documents/filename.pdf, NSFilePath=/Users/user/Library/Developer/CoreSimulator/Devices/A55EF2F7-FD8A-469C-A835-1A6282066F50/data/Containers/Data/Application/A00C3B10-8E12-44F1-B8F8-B53692CD4B56/Library/Caches/filename.pdf, NSUnderlyingError=0x600003682130 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
1

There are 1 best solutions below

0
On

I figured out that you have to share the file to save it to Files. I'll be using the share package to do this.