obsidian/笔记文件/2.笔记/安卓和ios读写文件权限.md

56 lines
2.0 KiB
Markdown
Raw Permalink Normal View History

2025-04-18 18:26:52 +08:00
#安卓
#ios
#unity/日常积累
如果是私有目录例如要操作unity对应数据持久化路径参考[[【Unity】数据持久化路径Application.persistentDataPath]],是不需要,额外的权限申请;
如果需要读写,公共目录,例如相册相关等路径,就需要额外申请,对应的权限
安卓的:
``` xml
<!-- AndroidManifest.xml 需要添加 -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
```
如果没有权限,也可以尝试,通过代码,动态获取一下
``` java
if (!Permission.HasUserAuthorizedPermission(Permission.ExternalStorageWrite))
{
Permission.RequestUserPermission(Permission.ExternalStorageWrite);
// 需要处理异步授权结果
}
```
如果是ios的话例如如果要申请公共相册相关的权限就需要在Info.plist里面添加
``` xml
<!-- Info.plist 需要添加 -->
<key>NSPhotoLibraryUsageDescription</key>
<string>需要访问相册来保存文件</string>
```
unity后处理参考[[xcode依赖处理 打包后处理通过podfile添加第三方库 PostProcessBuildClass]]
``` cs
/// <summary>
/// 添加广告追踪权限
/// </summary>
/// <param name="path"></param>
private static void AddInfoByTracking(string path)
{
string plistPath = Path.Combine(path, "Info.plist");
PlistDocument plist = new PlistDocument();
plist.ReadFromFile(plistPath);
PlistElementDict rootDict = plist.root;
// 添加广告追踪权限说明
rootDict.SetString("NSUserTrackingUsageDescription", "We need your consent to track advertisements to provide better services.");
// 添加定位服务权限说明
rootDict.SetString("NSLocationWhenInUseUsageDescription", "We need your location to provide personalized services.");
File.WriteAllText(plistPath, plist.WriteToString());
}
```