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

Unity中如何将PDF文件保存至iOS设备的Downloads文件夹

iOS Unity 导出PDF到系统Downloads文件夹解决方案

前置权限配置

首先需要在Xcode项目的Info.plist中添加以下配置,否则会出现权限错误或审核被拒:

  • 添加NSDownloadsFolderUsageDescription字段,值填写权限申请说明,例如*"需要访问下载文件夹以存储导出的PDF文件"*
  • 开启UIFileSharingEnabled(对应Xcode配置页的「Supports Document Browser」)为YES
  • 开启LSSupportsOpeningDocumentsInPlace为YES

核心实现代码

需要编写轻量iOS原生插件配合Unity C#逻辑实现,整体流程如下:

1. 原生OC插件代码

在Assets/Plugins/iOS目录下新建文件iOSDownloadHelper.m,写入以下内容:

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

extern "C" {
    // 获取系统Downloads文件夹路径
    char* GetiOSDownloadsDirectory() {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDownloadsDirectory, NSUserDomainMask, YES);
        NSString *downloadPath = paths.firstObject;
        return strdup(downloadPath.UTF8String);
    }

    // 显示下载完成提示,支持点击跳转查看文件
    void ShowDownloadCompleteNotification(NSString* fileName, NSString* fullPath) {
        dispatch_async(dispatch_get_main_queue(), ^{
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"下载完成" 
                message:[NSString stringWithFormat:@"%@ 已保存到系统下载文件夹", fileName] 
                preferredStyle:UIAlertControllerStyleAlert];
            
            // 查看按钮,点击跳转打开文件
            [alert addAction:[UIAlertAction actionWithTitle:@"查看" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                NSURL *fileUrl = [NSURL fileURLWithPath:fullPath];
                [[UIApplication sharedApplication] openURL:fileUrl options:@{} completionHandler:nil];
            }]];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil]];
            [UIApplication.sharedApplication.keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
        });
    }
}

2. Unity C#调用逻辑

using System.IO;
using UnityEngine;
using System.Runtime.InteropServices;

public class PDFExportManager : MonoBehaviour
{
    [DllImport("__Internal")]
    private static extern string GetiOSDownloadsDirectory();

    [DllImport("__Internal")]
    private static extern void ShowDownloadCompleteNotification(string fileName, string fullPath);

    /// <summary>
    /// 保存PDF到iOS系统Downloads文件夹
    /// </summary>
    /// <param name="pdfBytes">PDF文件的二进制数据</param>
    /// <param name="saveFileName">保存的文件名,需带.pdf后缀</param>
    public void SavePDFToSystemDownloads(byte[] pdfBytes, string saveFileName)
    {
#if UNITY_IOS && !UNITY_EDITOR
        // 获取系统Downloads路径
        string downloadDir = GetiOSDownloadsDirectory();
        string fullSavePath = Path.Combine(downloadDir, saveFileName);
        
        // 写入文件
        File.WriteAllBytes(fullSavePath, pdfBytes);
        
        // 触发系统下载提示
        ShowDownloadCompleteNotification(saveFileName, fullSavePath);
#else
        // 编辑器环境下测试逻辑,直接存到电脑的下载文件夹
        string testPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.UserProfile), 
            "Downloads", saveFileName);
        File.WriteAllBytes(testPath, pdfBytes);
        Debug.Log($"PDF测试保存完成,路径:{testPath}");
#endif
    }
}

兼容说明

  • 以上直接写入系统Downloads的方案仅支持iOS 13及以上版本
  • 若需要适配iOS 13以下版本,可以使用备选方案:将PDF存到应用沙盒的Documents目录,开启文件共享权限后用户可在Files app的应用目录下访问文件,示例代码:
string docsPath = Path.Combine(Application.persistentDataPath, "..", "Documents", saveFileName);
File.WriteAllBytes(docsPath, pdfBytes);
  • 所有方案都需要确保保存的文件名携带.pdf后缀,否则系统无法正确识别文件格式

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 23:54:04