You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Flutter在iPhone中创建自定义名称文件夹?iOS端实现遇阻

Flutter iOS 创建文件夹问题解决方案

关于模拟器路径的说明

iOS模拟器没有独立物理存储,它的应用目录本身就是映射到Mac本地的文件夹,所以getApplicationSupportDirectory()返回Mac路径是正常现象,并非bug。你可以通过这个路径直接访问模拟器内的文件,也可以通过Xcode的「Window > Devices and Simulators」选择对应模拟器和应用,点击「Download Container」查看完整的应用文件系统。

代码修正

你的代码存在两个潜在问题:一是未先创建目标文件夹,直接创建文件会因上级目录不存在而失败;二是如果需要用户在「文件」App中看到该文件夹,getApplicationSupportDirectory()属于应用私有目录,用户无法直接访问,需改用可共享目录。

方案1:使用应用私有目录(仅应用自身可访问)

if (!isTemporaryStorage) {
  if (Platform.isIOS) {
    var iosDir = await getApplicationSupportDirectory();
    // 先创建目标文件夹,recursive: true会自动创建多级目录
    final targetDir = Directory('${iosDir.path}/Majd_Eng_folder');
    if (!await targetDir.exists()) {
      await targetDir.create(recursive: true);
    }
    path = File('${targetDir.path}/$fileName');
  } else {
    final targetDir = Directory('/storage/emulated/0/Majd_Eng_folder');
    if (!await targetDir.exists()) {
      await targetDir.create(recursive: true);
    }
    path = File('${targetDir.path}/$fileName');
  }
}

方案2:使用可共享目录(用户可在「文件」App查看)

如果需要让用户能在系统「文件」App中看到这个文件夹,改用getApplicationDocumentsDirectory(),同时需配置iOS的Info.plist:

  1. 在ios/Runner/Info.plist中添加以下配置:
<key>UIFileSharingEnabled</key>
<true/>
<key>LSSupportsOpeningDocumentsInPlace</key>
<true/>
  1. 修改代码:
if (!isTemporaryStorage) {
  if (Platform.isIOS) {
    var iosDir = await getApplicationDocumentsDirectory();
    final targetDir = Directory('${iosDir.path}/Majd_Eng_folder');
    if (!await targetDir.exists()) {
      await targetDir.create(recursive: true);
    }
    path = File('${targetDir.path}/$fileName');
  } else {
    final targetDir = Directory('/storage/emulated/0/Majd_Eng_folder');
    if (!await targetDir.exists()) {
      await targetDir.create(recursive: true);
    }
    path = File('${targetDir.path}/$fileName');
  }
}

验证方法

  • 模拟器:可直接通过代码返回的路径在Mac上找到文件,或通过Xcode的Devices and Simulators查看应用容器。
  • 真机:若使用方案2,打开「文件」App,在「我的iPhone」下找到你的应用目录,即可看到Majd_Eng_folder。

内容的提问来源于stack exchange,提问作者ala abdelhadi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 08:15:32