killapp/Assets/Editor/iOSWiFiBuildPostProcessor.cs

46 lines
1.7 KiB
C#
Raw Permalink Normal View History

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
{
[PostProcessBuild]
public static void OnPostProcessBuild(BuildTarget target, string pathToBuiltProject)
{
if (target != BuildTarget.iOS) return;
string plistPath = Path.Combine(pathToBuiltProject, "Info.plist");
PlistDocument plist = new PlistDocument();
plist.ReadFromString(File.ReadAllText(plistPath));
PlistElementDict rootDict = plist.root;
// 添加位置权限描述iOS 获取 WiFi 需要)
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());
// 添加 CoreBluetooth.framework
string projPath = PBXProject.GetPBXProjectPath(pathToBuiltProject);
PBXProject proj = new PBXProject();
proj.ReadFromString(File.ReadAllText(projPath));
string targetGuid = proj.GetUnityMainTargetGuid();
proj.AddFrameworkToProject(targetGuid, "CoreBluetooth.framework", false);
File.WriteAllText(projPath, proj.WriteToString());
}
}
#endif