How to save a text file in external storage in ios using flutter?

14,549

Solution 1

You can save the file in the NSDocumentsDirectory (getApplicationDocumentsDirectory()) and then make it available to the user.

From: https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html

Use this directory to store user-generated content. The contents of this directory can be made available to the user through file sharing; therefore, his directory should only contain files that you may wish to expose to the user.

To make the directory available to the user you need to open the Xcode project under 'your_app/ios/Runner.xcworkspace'. Then open the Info.plist file in the Runner directory and add two rows with the key UIFileSharingEnabled and LSSupportsOpeningDocumentsInPlace. Set the value of both keys to YES.

If you now open the Files app and click on 'On My iPhone' you should see a folder with the name of your application.

Solution 2

As a compliment do @ni. answer, you must enable both keys. This is what it should look on:

Xcode (info.plist):

Info.plist on Xcode

Editor (info.plist):

<key>LSSupportsOpeningDocumentsInPlace</key>
<true/>
<key>UIFileSharingEnabled</key>
<true/>

Info in source:

In iOS 11 and later, if both this keys LSSupportsOpeningDocumentsInPlace and the UIFileSharingEnabled key are YES, the local file provider grants access to all the documents in the app’s Documents directory. These documents appear in the Files app, and in a document browser. Users can open and edit these document in place.

Source: Apple developer - iOS keys

Solution 3

I have used below code to fix this.

Directory directory = Platform.isAndroid
    ? await getExternalStorageDirectory() //FOR ANDROID
    : await getApplicationSupportDirectory(); //FOR iOS

also add those files as mentioned by @Gabrielhn

Share:
14,549
Keshav
Author by

Keshav

Updated on June 03, 2022

Comments

  • Keshav
    Keshav almost 2 years

    I have created an app in which user can create and save a text file in the local storage of Android. I have used path_provider package which gives getExternalStorageDirectory() to save the data in the external storage. But I can not find the same for ios. getExternalStorageDirectory() is not supported for ios.

    I want to save the file so that user can later on access the file from Files app in ios.enter image description here

    Future<String> get _localPath async{
    var dir = await getExternalStorageDirectory();
    
      return dir.path;
    }
    
    Future<File> get _localFile async{
      final path = await _localPath;
    
      return File('$path/test.txt');
    }
    
    Future<File> writeData(String msg) async{
      final file = await _localFile;
    
      return file.writeAsString(msg);
    }
    

    The above code works for Android(if storage permissions are provided). Can anyone help me for achieving the same in ios.

  • nani
    nani over 3 years
    Thx for the answer, but the problem with this solution, is the user gets also access to all the other files, like db and json files. Is there a way to only deny access to those?
  • Andres Naranjo
    Andres Naranjo about 3 years
    Thanks! It helped me
  • BlueRaja - Danny Pflughoeft
    BlueRaja - Danny Pflughoeft over 2 years
    @nani Those should go in the library directory, not the documents directory