如何使用flutter将文本文件保存在ios的外部存储中? [英] How to save a text file in external storage in ios using flutter?

查看:1100
本文介绍了如何使用flutter将文本文件保存在ios的外部存储中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我要保存文件,以便用户以后可以使用从ios中的 Files应用 访问文件。

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

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中实现同样的功能。

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

推荐答案

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

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

来自: 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.

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

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.

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

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

这篇关于如何使用flutter将文本文件保存在ios的外部存储中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆