Flutter Dio下载文件,共享到苹果手机文件夹

我们在安卓里面,下载文件以后,打开文件夹就可以看到,但是苹果手机上面打开文件夹,看不到应用,

想要在苹果手机上显示应用的文件夹,那要怎么做呢?



1,dio下载文件保存到本地

ios设置文件夹是 getTemporaryDirectory(),不要使用getApplicationDocumentsDirectory()看不到

getTemporaryDirectory()默认目录是Library/Caches,这个目录没有暴露

暴露的目录是Documents

将Library/Caches替换为Documents

dirloc= dirloc.replaceFirst("Library/Caches", "Documents/");

Future downloadSaveFile(EmailContent emailContent, int index) async {

    if (ObjectUtil.isNotEmpty(emailContent.attachments[index].download) &&

        emailContent.attachments[index].download) {

      FilePickerResult result =

          await FilePicker.platform.pickFiles(allowMultiple: false);

      return;

    }

    final FileSystem fs = MemoryFileSystem();

    Map user = await SharedPrefercnes.getString('email_user');

    userInfo = EmailUser.fromJson(user);

    Attachments attachments = emailContent.attachments[index];

    String downloadFile =

        '${Api.download_file}/?raw_name=${widget.mailBoxType}&uid=${widget.uid}&sid=${attachments.sid}';

    Map headers = {

      "Authorization": "JWT ${userInfo.data.emailToken}",

      'Accept': 'application/json, text/plain, */*',

    };

    String url = Api.ip + downloadFile;

    Dio dio = Dio();

    PermissionStatus status = await PermissionHandler()

        .checkPermissionStatus(PermissionGroup.storage);

    //判断如果还没拥有读写权限就申请获取权限

    if (status != PermissionStatus.granted) {

      var map = await PermissionHandler()

          .requestPermissions([PermissionGroup.storage]).then((value) {

        print(value);

      });

      if (map[PermissionGroup.storage] == PermissionStatus.granted) {

        status = PermissionStatus.granted;

      }

    }

    print(attachments.name);

    if (true) {

      String dirloc = "";

      if (Platform.isAndroid) {

        dirloc = "/sdcard/download/";

      } else {

        dirloc = (await getTemporaryDirectory()).path;

      dirloc= dirloc.replaceFirst("Library/Caches", "Documents/");

      }

      var randid = random.nextInt(10000);

      try {

        //2、创建文件

        FileUtils.mkdir([dirloc]);

        String token = "JWT ${userInfo.data.emailToken}";

        var options = Options(headers: {

          "Authorization": token,

        });

        print(url);

        //3、使用 dio 下载文件

        await dio.download(url, dirloc + attachments.name, options: options,

            onReceiveProgress: (receivedBytes, totalBytes) {

          setState(() {

            downloading = true;

            //4、连接资源成功开始下载后更新状态

            progress = (receivedBytes / totalBytes);

          });

        });

      } catch (e) {

        print(e);

      }

      // setState(() async {

      downloading = false;

      progress = 0;

      path = dirloc + attachments.name;

      print(path);

      emailContent.attachments[index].download = true;

      ToastUtils.getInstance().showToast("下载完成,请在文件夹中查看");

      // });

    } else {

      setState(() {

        progress = 0;

        _onPressed = () {

          downloadSaveFile(emailContent, index);

        };

      });

    }

  }

2,共享应用目录到苹果手机文件夹,

在info.list 添加Supports Document Browser ,

Supports opening documents in place,

UIFileSharingEnabled,

设置为true。

大功告成,打开苹果文件夹,就可以看到本应用的目录,打开目录就看到应用下载的文件啦!

你可能感兴趣的:(Flutter Dio下载文件,共享到苹果手机文件夹)