2026-04-28 16:35:51 +08:00
|
|
|
|
#if UNITY_IOS
|
|
|
|
|
|
using UnityEditor;
|
|
|
|
|
|
using UnityEditor.Callbacks;
|
|
|
|
|
|
using UnityEditor.iOS.Xcode;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
|
|
|
|
public class iOSWiFiBuildPostProcessor
|
|
|
|
|
|
{
|
2026-07-06 15:40:04 +08:00
|
|
|
|
private const string WIFI_INFO_ENTITLEMENT = "com.apple.developer.networking.wifi-info";
|
|
|
|
|
|
|
2026-04-28 16:35:51 +08:00
|
|
|
|
[PostProcessBuild]
|
|
|
|
|
|
public static void OnPostProcessBuild(BuildTarget target, string pathToBuiltProject)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (target != BuildTarget.iOS) return;
|
|
|
|
|
|
|
2026-07-06 15:40:04 +08:00
|
|
|
|
// ===== 1. 配置 Info.plist =====
|
2026-04-28 16:35:51 +08:00
|
|
|
|
string plistPath = Path.Combine(pathToBuiltProject, "Info.plist");
|
|
|
|
|
|
PlistDocument plist = new PlistDocument();
|
|
|
|
|
|
plist.ReadFromString(File.ReadAllText(plistPath));
|
|
|
|
|
|
|
|
|
|
|
|
PlistElementDict rootDict = plist.root;
|
|
|
|
|
|
|
2026-07-06 15:40:04 +08:00
|
|
|
|
// 添加位置权限描述(iOS 13+ 获取 WiFi 需要)
|
2026-04-28 16:35:51 +08:00
|
|
|
|
rootDict.SetString("NSLocationWhenInUseUsageDescription", "需要位置权限来获取当前 WiFi 信息");
|
|
|
|
|
|
|
|
|
|
|
|
// 添加蓝牙权限描述
|
|
|
|
|
|
rootDict.SetString("NSBluetoothAlwaysUsageDescription", "需要蓝牙权限来连接设备");
|
|
|
|
|
|
rootDict.SetString("NSBluetoothPeripheralUsageDescription", "需要蓝牙权限来连接设备");
|
|
|
|
|
|
|
|
|
|
|
|
// 添加后台模式
|
|
|
|
|
|
PlistElementArray bgModes = rootDict.CreateArray("UIBackgroundModes");
|
|
|
|
|
|
bgModes.AddString("fetch");
|
|
|
|
|
|
bgModes.AddString("bluetooth-central");
|
|
|
|
|
|
|
|
|
|
|
|
File.WriteAllText(plistPath, plist.WriteToString());
|
2026-07-06 15:40:04 +08:00
|
|
|
|
|
|
|
|
|
|
// ===== 2. 配置 Xcode 项目 =====
|
2026-04-28 16:35:51 +08:00
|
|
|
|
string projPath = PBXProject.GetPBXProjectPath(pathToBuiltProject);
|
|
|
|
|
|
PBXProject proj = new PBXProject();
|
|
|
|
|
|
proj.ReadFromString(File.ReadAllText(projPath));
|
|
|
|
|
|
string targetGuid = proj.GetUnityMainTargetGuid();
|
|
|
|
|
|
|
|
|
|
|
|
proj.AddFrameworkToProject(targetGuid, "CoreBluetooth.framework", false);
|
2026-07-06 15:40:04 +08:00
|
|
|
|
|
|
|
|
|
|
// ===== 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);
|
2026-04-28 16:35:51 +08:00
|
|
|
|
|
2026-07-06 15:40:04 +08:00
|
|
|
|
// 同时设置 framework target
|
|
|
|
|
|
string frameworkTargetGuid = proj.GetUnityFrameworkTargetGuid();
|
|
|
|
|
|
if (!string.IsNullOrEmpty(frameworkTargetGuid))
|
|
|
|
|
|
{
|
|
|
|
|
|
proj.SetBuildProperty(frameworkTargetGuid, "CODE_SIGN_ENTITLEMENTS", entitlementsFileName);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-28 16:35:51 +08:00
|
|
|
|
File.WriteAllText(projPath, proj.WriteToString());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#endif
|