killapp/Assets/Editor/iOSWiFiBuildPostProcessor.cs
yuqucheng 9316f8f331 feat(iOS): 完善iOS平台WiFi配对与权限本地化适配
1. 新增iOSWiFiHelper类实现异步获取WiFi SSID,自动请求位置权限
2. 新增iOS权限描述本地化处理器,实现中英文权限文案自动切换
3. 更新所有iOS权限描述为更清晰的英文/中文说明
4. 优化iOS编译后处理脚本优先级,避免被其他插件覆盖
5. 在连接设备页面和WiFi设置页面自动填充当前WiFi名称
6. 更新应用版本号与构建号
2026-08-07 16:14:10 +08:00

79 lines
3.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#if UNITY_IOS
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;
using System.IO;
public class iOSWiFiBuildPostProcessor
{
private const string WIFI_INFO_ENTITLEMENT = "com.apple.developer.networking.wifi-info";
// 高于默认顺序执行,确保覆盖其他插件写入的蓝牙描述
[PostProcessBuild(1000)]
public static void OnPostProcessBuild(BuildTarget target, string pathToBuiltProject)
{
if (target != BuildTarget.iOS) return;
// ===== 1. 配置 Info.plist =====
string plistPath = Path.Combine(pathToBuiltProject, "Info.plist");
PlistDocument plist = new PlistDocument();
plist.ReadFromString(File.ReadAllText(plistPath));
PlistElementDict rootDict = plist.root;
// 添加位置权限描述iOS 13+ 获取 WiFi 需要)
rootDict.SetString("NSLocationWhenInUseUsageDescription", "Location access is used to detect the current WiFi network so we can display the connected WiFi name.");
// 添加蓝牙权限描述
rootDict.SetString("NSBluetoothAlwaysUsageDescription", "Bluetooth is used to scan for and connect to your device. For example, after pairing with the device, we send control commands to it.");
rootDict.SetString("NSBluetoothPeripheralUsageDescription", "Bluetooth is used to scan for and connect to your device. For example, after pairing with the device, we send control commands to it.");
// 添加后台模式
PlistElementArray bgModes = rootDict.CreateArray("UIBackgroundModes");
bgModes.AddString("fetch");
bgModes.AddString("bluetooth-central");
File.WriteAllText(plistPath, plist.WriteToString());
// ===== 2. 配置 Xcode 项目 =====
string projPath = PBXProject.GetPBXProjectPath(pathToBuiltProject);
PBXProject proj = new PBXProject();
proj.ReadFromString(File.ReadAllText(projPath));
string targetGuid = proj.GetUnityMainTargetGuid();
proj.AddFrameworkToProject(targetGuid, "CoreBluetooth.framework", false);
// ===== 3. 添加 Access WiFi Information 权限iOS 14+ 必需)=====
string entitlementsFileName = "app.entitlements";
string entitlementsPath = Path.Combine(pathToBuiltProject, entitlementsFileName);
// 创建或读取 entitlements 文件
PlistDocument entitlements = new PlistDocument();
if (File.Exists(entitlementsPath))
{
entitlements.ReadFromString(File.ReadAllText(entitlementsPath));
}
else
{
entitlements.root.SetString("aps-environment", "development");
}
// 添加 WiFi 信息访问权限iOS 14+
entitlements.root.SetBoolean(WIFI_INFO_ENTITLEMENT, true);
File.WriteAllText(entitlementsPath, entitlements.WriteToString());
// 设置 entitlements 文件到 Xcode 项目
proj.SetBuildProperty(targetGuid, "CODE_SIGN_ENTITLEMENTS", entitlementsFileName);
// 同时设置 framework target
string frameworkTargetGuid = proj.GetUnityFrameworkTargetGuid();
if (!string.IsNullOrEmpty(frameworkTargetGuid))
{
proj.SetBuildProperty(frameworkTargetGuid, "CODE_SIGN_ENTITLEMENTS", entitlementsFileName);
}
File.WriteAllText(projPath, proj.WriteToString());
}
}
#endif