如何使用flutter将文本文件保存在ios的外部存储中?

时间:2019-03-18 11:44:58

标签: ios file dart flutter storage

我创建了一个应用程序,用户可以在其中创建文本文件并将其保存在Android的本地存储中。我使用了 path_provider 包,该包提供了 getExternalStorageDirectory() 来将数据保存在外部存储中。但是我找不到 ios 的相同内容。 ios不支持 getExternalStorageDirectory()

我想保存文件,以便用户以后可以从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);
}

以上代码适用于Android(如果提供了存储权限)。谁能帮助我在ios中实现同样的目标。

2 个答案:

答案 0 :(得分:2)

夸奖@ni。答案,您必须同时启用两个键。这就是它的外观:

Xcode(info.plist):

Info.plist on Xcode

编辑器(info.plist):

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

来源信息:

  

在iOS 11和更高版本中,如果两个键 LSSupportsOpeningDocumentsInPlace UIFileSharingEnabled 键均为YES,则本地文件提供者将授予对应用程序“文档”目录中所有文档的访问权限。这些文档显示在“文件”应用程序和文档浏览器中。用户可以就地打开和编辑这些文档。

来源:Apple developer - iOS keys

答案 1 :(得分:0)

您可以将文件保存在NSDocumentsDirectory(getApplicationDocumentsDirectory())中,然后将其提供给用户。

发件人:https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html

  

使用此目录存储用户生成的内容。可以通过文件共享使该目录的内容对用户可用。因此,他的目录应该只包含您可能希望向用户公开的文件。

要使目录对用户可用,您需要在“ your_app / ios / Runner.xcworkspace”下打开Xcode项目。然后在Runner目录中打开Info.plist文件,并用键UIFileSharingEnabledLSSupportsOpeningDocumentsInPlace添加两行。将两个键的值都设置为YES

如果现在打开“文件”应用程序并单击“在我的iPhone上”,您应该会看到一个包含应用程序名称的文件夹。