Handling PathAccessException in iOS for File Download

In my Flutter application, I have implemented a feature that allows users to download a document and save it to a location of their choice. The process involves:

  • Using the FilePicker package to let the user select a directory.
  • Saving the file using File.writeAsBytes after the directory is selected.

This approach works perfectly on Android, but on iOS, I encounter a PathAccessException when attempting to save the file to the selected directory, likely due to platform-specific restrictions on file system access.

Below is the code for reference:

final String selectedDirectory = await FilePicker.platform.getDirectoryPath() ?? '';
if (selectedDirectory.isNotEmpty) {
  final String filePath = '$selectedDirectory/$fileName$ext';
  final File file = File(filePath);
  await file.writeAsBytes(fileData);
}

What changes or permissions are required to enable writing files to a user-selected directory on iOS? Is there an alternative approach to achieve this functionality on iOS?

Does this happen the first time you download an image already?

Hi @escamoteur, This issue occurs every time I attempt a download.

Then it is probably a permission problem. I don’t know which folder the FilePicker offers you on iOS but if it is outside of your apps folder you surely need additional permissions

How can I request user permissions on iOS, or should I add permissions in the iOS project files?

I guess you have to add some settings in your ios project file but would have to search for it too. I would try searching for the same problem on ios in general not with focus on Flutter.

1 Like