如何使用 flutter 将文本文件保存在 ios 的外部存储中?
How to save a text file in external storage in ios using flutter?
我创建了一个应用程序,用户可以在其中创建文本文件并将其保存在 Android 的本地存储中。我使用了 path_provider 包,它提供 getExternalStorageDirectory() 将数据保存在外置储存。但是我找不到 ios 的相同内容。 getExternalStorageDirectory() 不支持 ios.
我想保存该文件,以便用户以后可以从 ios 中的 Files 应用程序 访问该文件。
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);
}
以上代码适用于 Android(如果提供了存储权限)。谁能帮助我在 ios.
中实现同样的目标
您可以将文件保存在 NSDocumentsDirectory (getApplicationDocumentsDirectory()
) 中,然后将其提供给用户。
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.
要使目录对用户可用,您需要打开 'your_app/ios/Runner.xcworkspace' 下的 Xcode 项目。然后打开Runner目录下的Info.plist文件,添加两行key为UIFileSharingEnabled
和LSSupportsOpeningDocumentsInPlace
。将两个键的值设置为 YES
.
如果您现在打开“文件”应用并单击 'On My iPhone',您应该会看到一个与您的应用同名的文件夹。
@ni 作为赞美。回答,您必须同时启用这两个键。这是它应该看到的:
Xcode (info.plist):
Info.plist on Xcode
编辑(info.plist):
<key>LSSupportsOpeningDocumentsInPlace</key>
<true/>
<key>UIFileSharingEnabled</key>
<true/>
来源信息:
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.
我已经使用下面的代码来解决这个问题。
Directory directory = Platform.isAndroid
? await getExternalStorageDirectory() //FOR ANDROID
: await getApplicationSupportDirectory(); //FOR iOS
还添加@Gabrielhn 提到的那些文件
我创建了一个应用程序,用户可以在其中创建文本文件并将其保存在 Android 的本地存储中。我使用了 path_provider 包,它提供 getExternalStorageDirectory() 将数据保存在外置储存。但是我找不到 ios 的相同内容。 getExternalStorageDirectory() 不支持 ios.
我想保存该文件,以便用户以后可以从 ios 中的 Files 应用程序 访问该文件。
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);
}
以上代码适用于 Android(如果提供了存储权限)。谁能帮助我在 ios.
中实现同样的目标您可以将文件保存在 NSDocumentsDirectory (getApplicationDocumentsDirectory()
) 中,然后将其提供给用户。
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.
要使目录对用户可用,您需要打开 'your_app/ios/Runner.xcworkspace' 下的 Xcode 项目。然后打开Runner目录下的Info.plist文件,添加两行key为UIFileSharingEnabled
和LSSupportsOpeningDocumentsInPlace
。将两个键的值设置为 YES
.
如果您现在打开“文件”应用并单击 'On My iPhone',您应该会看到一个与您的应用同名的文件夹。
@ni 作为赞美。回答,您必须同时启用这两个键。这是它应该看到的:
Xcode (info.plist):
Info.plist on Xcode
编辑(info.plist):
<key>LSSupportsOpeningDocumentsInPlace</key>
<true/>
<key>UIFileSharingEnabled</key>
<true/>
来源信息:
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.
我已经使用下面的代码来解决这个问题。
Directory directory = Platform.isAndroid
? await getExternalStorageDirectory() //FOR ANDROID
: await getApplicationSupportDirectory(); //FOR iOS
还添加@Gabrielhn 提到的那些文件