如何用C#实现代码打开iOS 11的Files app并跳转至应用Documents目录
iOS 跳转到系统Files应用对应目录的C#实现
前置配置
首先需要在项目的Info.plist文件中添加两个配置项,开启应用目录的公开访问权限,否则你的应用不会出现在Files应用的「我的iPhone」列表中:
<key>UIFileSharingEnabled</key> <true/> <key>LSSupportsOpeningDocumentsInPlace</key> <true/>
如果不需要支持用户在Files中直接修改你应用内的文件,可以将LSSupportsOpeningDocumentsInPlace设置为false,不影响跳转逻辑。
核心实现代码
以下代码适用于Xamarin.iOS或MAUI iOS平台:
using Foundation; using UIKit; public void JumpToAppDocumentsInFiles() { // 获取当前应用Documents目录的本地路径 string docPath = NSSearchPath.GetDirectories( NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomain.User, true ).FirstOrDefault(); if (string.IsNullOrEmpty(docPath)) return; // 拼接Files应用专属的跳转Scheme NSUrl targetUrl = NSUrl.FromString($"shareddocuments://{docPath}"); // 校验跳转可用性后执行跳转 if (UIApplication.SharedApplication.CanOpenUrl(targetUrl)) { UIApplication.SharedApplication.OpenUrl(targetUrl, new NSDictionary(), null); } }
注意事项
- 该功能仅支持iOS 11及以上系统,调用前建议先判断系统版本,避免低版本系统出现异常
- 如果是MAUI跨平台项目,可通过依赖服务、平台条件编译的方式调用上述iOS平台专属代码
内容的提问来源于stack exchange,提问作者George
相关产品推荐
相关产品推荐

