tauri-apps / plugins-workspace

All of the official Tauri plugins in one place!
https://tauri.app
Apache License 2.0
942 stars 265 forks source link

[plugin-dialog][plugin-fs] WriteTextFile does not work with iOS #1976

Open rtiagom opened 1 week ago

rtiagom commented 1 week ago

writeTextFile does not work with iOS. The code completes without any errors, but the iOS simulator and actual device show an empty file.

 save().then(filePath => {
      console.log(filePath);
      if (filePath) {
            const contents = JSON.stringify({ notifications: true });
           writeTextFile(filePath, contents, {baseDir: fsModule.BaseDirectory.Home}).then(() => {
                console.log("done");
                this._toastr.success("Done");
            }).catch(e => {
                console.log(e);
            })
      }
  });
[✔] Environment
    - OS: Mac OS 15.0.1 arm64 (X64)
    ✔ Xcode Command Line Tools: installed
    ✔ rustc: 1.82.0 (f6e511eec 2024-10-15)
    ✔ cargo: 1.82.0 (8f40fc59f 2024-08-21)
    ✔ rustup: 1.27.1 (54dd3d00f 2024-04-24)
    ✔ Rust toolchain: stable-aarch64-apple-darwin (default)
    - node: 20.17.0
    - pnpm: 9.11.0
    - npm: 10.8.2

[-] Packages
    - tauri 🦀: 2.0.6
    - tauri-build 🦀: 2.0.2
    - wry 🦀: 0.46.3
    - tao 🦀: 0.30.3
    - @tauri-apps/api : 2.0.3
    - @tauri-apps/cli : 2.0.3 (outdated, latest: 2.0.4)

[-] Plugins
    - tauri-plugin-store 🦀: 2.1.0
    - @tauri-apps/plugin-store : not installed!
    - tauri-plugin-fs 🦀: 2.0.3
    - @tauri-apps/plugin-fs : not installed!
    - tauri-plugin-dialog 🦀: 2.0.3
    - @tauri-apps/plugin-dialog : not installed!
    - tauri-plugin-log 🦀: 2.0.1
    - @tauri-apps/plugin-log : not installed!
    - tauri-plugin-window-state 🦀: 2.0.1
    - @tauri-apps/plugin-window-state : not installed!
    - tauri-plugin-shell 🦀: 2.0.2
    - @tauri-apps/plugin-shell : not installed!
    - tauri-plugin-notification 🦀: 2.0.1
    - @tauri-apps/plugin-notification : not installed!
rtiagom commented 1 week ago

I was able to make it work by creating a Swift plugin that accepts the filename and the contents. With the plugin-fs, before I call writeTextFile, I also call save() from plugin-dialog and get a file URL that I pass to writeTextFile. The issue might be with either plugin-fs or plugin-dialog.

Both plugin-fs and plugin-dialog say that they support iOS.

    @objc public func write(_ invoke: Invoke) throws {
        logger.log("Write To File function call")
        let args = try invoke.parseArgs(WriteFsArgs.self)
        let filename = args.filename;
        let fileManager = FileManager.default
        guard let documentsURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first else {
            logger.log("Could not locate Documents directory")
            invoke.reject("Could not locate Documents directory")
            return
        }

        let fileURL = documentsURL.appendingPathComponent(filename)
        do {
            try args.content.write(to: fileURL, atomically: true, encoding: .utf8)
            logger.log("File written successfully to \(fileURL.path)")
            invoke.resolve(["success": 1])
        } catch {
            logger.log("Failed to write to file: \(error.localizedDescription)")
            invoke.resolve(["success": 0])
        }
    }
rtiagom commented 1 week ago

After some testing, the problem seems to lie with plugin-dialog. Calling save dialog returns a file path that is unusable with writeTextFile in iOS