HealthKit功能不是通过unity c#脚本添加的
有人能帮我解决unity应用程序的healthKit功能问题吗。 我正在尝试将healthKit功能添加到我的unity应用程序中。我正在使用BEHEALTHKIT和HealthKitBuildProcessor.cs编辑器类添加功能和其他依赖项。以下是我正在使用的代码。但由于某些原因,healthkit功能和权限没有通过此代码添加(权限参数正在添加到plist),并且在打印Debug.Log时返回null(“NewRightages:+NewRightages”);我的构建也失败了,错误是“配置文件不支持HealthKit功能” 我已经为developer.apple.com上的配置文件添加了HealthKit功能。 统一版本:2019.4.4f1HealthKit功能不是通过unity c#脚本添加的,c#,ios,unity3d,healthkit,C#,Ios,Unity3d,Healthkit,有人能帮我解决unity应用程序的healthKit功能问题吗。 我正在尝试将healthKit功能添加到我的unity应用程序中。我正在使用BEHEALTHKIT和HealthKitBuildProcessor.cs编辑器类添加功能和其他依赖项。以下是我正在使用的代码。但由于某些原因,healthkit功能和权限没有通过此代码添加(权限参数正在添加到plist),并且在打印Debug.Log时返回null(“NewRightages:+NewRightages”);我的构建也失败了,错误是“配
public class HealthKitBuildProcessor : IProcessSceneWithReport
{
private static string shareString = null;
private static string updateString = null;
private static string clinicalString = null;
/*! @brief required by the IProcessScene interface. Set high to let other postprocess scripts run first. */
public int callbackOrder {
get { return 100; }
}
/*! @brief Searches for HealthKitDataTypes objects & reads the usage strings for the OnPostprocessBuild phase.
@param scene the scene being processed.
@param report a report containing information about the current build
*/
public void OnProcessScene(Scene scene, BuildReport report) {
GameObject[] rootObjects = scene.GetRootGameObjects();
foreach (GameObject obj in rootObjects) {
HealthKitDataTypes types = obj.GetComponentInChildren<HealthKitDataTypes>();
if (types != null) {
if (types.AskForSharePermission()) {
HealthKitBuildProcessor.shareString = types.healthShareUsageDescription;
}
if (types.AskForUpdatePermission()) {
HealthKitBuildProcessor.updateString = types.healthUpdateUsageDescription;
}
/*if (types.AskForClinicalPermission()) {
HealthKitBuildProcessor.clinicalString = types.clinicalUsageDescription;
}*/
}
}
}
/*! @brief Updates the Xcode project.
@param buildTarget the target build platform
@param path the path of the target build
*/
[PostProcessBuildAttribute(10)]
public static void OnPostprocessBuild(BuildTarget buildTarget, string path) {
Debug.Log("--- BEHEALTHKIT POST-PROCESS BUILD ---");
if (buildTarget == BuildTarget.iOS) {
//string projPath = path + "/Unity-iPhone.xcodeproj/project.pbxproj";
//Debug.Log("BE:PROJECT PATH :" + projPath);
var projPath = PBXProject.GetPBXProjectPath(path);
var proj = new PBXProject();
proj.ReadFromString(System.IO.File.ReadAllText(projPath));
#if UNITY_2019_3_OR_NEWER
string mainTarget = proj.GetUnityMainTargetGuid();
string frameworkTarget = proj.GetUnityFrameworkTargetGuid();
Debug.Log("--- BE: UNITY_2019_3_OR_NEWER ---");
Debug.LogFormat("main target: {0}", mainTarget);
Debug.LogFormat("framework target: {0}", frameworkTarget);
#else
string targetName = PBXProject.GetUnityTargetName();
string mainTarget = proj.TargetGuidByName(targetName);
Debug.Log("---BE: ELSE UNITY_2019_3_OR_NEWER ---");
Debug.Log("main target: {0}", mainTarget);
Debug.Log("targetName: ", targetName);
#endif
bool addHealthRecordsCapability = (clinicalString != null);
//Debug.Log("addHealthRecordsCapability: ", addHealthRecordsCapability);
// Info.plist
//-----------
Debug.Log("---BE: PLIST ---");
var info = ProcessInfoPList(path, addHealthRecordsCapability);
// Entitlements
//--------------
Debug.Log("---BE: ProcessEntitlements ---");
string entitlementsRelative = ProcessEntitlements(path, proj, mainTarget, info, addHealthRecordsCapability);
#if UNITY_2019_3_OR_NEWER
// add HealthKit capability
Debug.Log("------projPath "+projPath);
ProjectCapabilityManager capabilities = new ProjectCapabilityManager(projPath, "Entitlements.entitlements", null, mainTarget);
capabilities.AddHealthKit();
Debug.Log("---BE:Capability UNITY_2019_3_OR_NEWER ---");
// add HealthKit Framework
//proj.AddFrameworkToProject(frameworkTarget, "HealthKit.framework", true);
// Set a custom link flag
//proj.AddBuildProperty(frameworkTarget, "OTHER_LDFLAGS", "-ObjC");
#else
// add HealthKit capability
Debug.Log("---ELSE BE:Capability UNITY_2019_3_OR_NEWER ---");
Debug.Log("projectPath:" + projPath);
Debug.Log("entitlementsRelative:" + entitlementsRelative);
Debug.Log("targetName:" + targetName);
ProjectCapabilityManager capabilities = new ProjectCapabilityManager(projPath, entitlementsRelative, targetName);
capabilities.AddHealthKit();
// add HealthKit Framework
proj.AddFrameworkToProject(mainTarget, "HealthKit.framework", true);
// Set a custom link flag
proj.AddBuildProperty(mainTarget, "OTHER_LDFLAGS", "-ObjC");
#endif
proj.WriteToFile(projPath);
}
}
// -------------------------------
internal static PlistDocument ProcessInfoPList(string path, bool addHealthRecordsCapability) {
string plistPath = Path.Combine(path, "Info.plist");
PlistDocument info = GetInfoPlist(plistPath);
PlistElementDict rootDict = info.root;
// // Add the keys
if (HealthKitBuildProcessor.shareString != null) {
rootDict.SetString("NSHealthShareUsageDescription", HealthKitBuildProcessor.shareString);
}
else {
Debug.LogError("unable to read NSHealthShareUsageDescription");
}
if (HealthKitBuildProcessor.updateString != null) {
rootDict.SetString("NSHealthUpdateUsageDescription", HealthKitBuildProcessor.updateString);
}
if (addHealthRecordsCapability) {
rootDict.SetString("NSHealthClinicalHealthRecordsShareUsageDescription", HealthKitBuildProcessor.clinicalString);
}
// Write the file
info.WriteToFile(plistPath);
return info;
}
internal static string ProcessEntitlements(string path, PBXProject proj, string target, PlistDocument info, bool addHealthRecordsCapability) {
string entitlementsFile;
string entitlementsRelative;
string entitlementsPath;
Debug.Log("PATH: " + path);
Debug.Log("TARGET: " + target);
String test= proj.GetUnityMainTargetGuid();
Debug.Log("TEST proj: " + test);
entitlementsRelative = proj.GetBuildPropertyForConfig(target, "CODE_SIGN_ENTITLEMENTS");
Debug.Log("entitlementsRelative: " + entitlementsRelative);
Debug.LogFormat("get build property [{0}, {1} = {2}]", target, "CODE_SIGN_ENTITLEMENTS", entitlementsRelative);
PlistDocument entitlements = new PlistDocument();
if (entitlementsRelative == null) {
string projectname = GetProjectName(info);
Debug.Log("projectname: " + projectname);
entitlementsFile = Path.ChangeExtension("Entitlements", "entitlements");
Debug.Log("entitlementsFile: " + entitlementsFile);
entitlementsRelative = Path.Combine(path, entitlementsFile);
Debug.Log("entitlementsRelative: " + entitlementsRelative);
entitlementsPath = Path.Combine(path, entitlementsRelative);
Debug.Log("entitlementsPath: " + entitlementsPath);
//proj.AddFileToBuild(target, proj.AddFile(entitlementsRelative, entitlementsRelative, PBXSourceTree.Source));
Debug.LogFormat("add build property [{0}, {1}] => {2}", target, "CODE_SIGN_ENTITLEMENTS", entitlementsRelative);
proj.AddBuildProperty(target, "CODE_SIGN_ENTITLEMENTS", entitlementsFile);
string newEntitlements = proj.GetBuildPropertyForConfig(target, "CODE_SIGN_ENTITLEMENTS");
Debug.Log("newEntitlements: " + newEntitlements);
Debug.LogFormat("=> {0}", newEntitlements);
}
else {
entitlementsPath = Path.Combine(path, entitlementsRelative);
Debug.Log("ELSE:entitlementsPath " + entitlementsPath);
}
ReadEntitlements(entitlements, entitlementsPath);
entitlements.root.SetBoolean("com.apple.developer.healthkit", true);
if (addHealthRecordsCapability) {
Debug.Log("addHealthRecordsCapability =TRUE ");
var healthkitAccess = entitlements.root.CreateArray("com.apple.developer.healthkit.access");
healthkitAccess.AddString("health-records");
}
SaveEntitlements(entitlements, entitlementsPath);
return entitlementsRelative;
}
// -------------------------------
internal static void ReadEntitlements(PlistDocument entitlements, string destinationPath) {
Debug.Log("READING Entitlements [ReadEntitlements]");
Debug.Log("READING from destinationPath [ReadEntitlements]"+ destinationPath);
if (System.IO.File.Exists(destinationPath)) {
try {
Debug.LogFormat("reading existing entitlements: '{0}'.", destinationPath);
entitlements.ReadFromFile(destinationPath);
}
catch (Exception e) {
Debug.LogErrorFormat("error reading from file: {0}", e);
}
}
}
internal static void SaveEntitlements(PlistDocument entitlements, string destinationPath) {
try {
Debug.Log("----SaveEntitlements---");
entitlements.WriteToFile(destinationPath);
}
catch (Exception e) {
Debug.LogErrorFormat("error writing to file: {0}", e);
}
}
internal static PlistDocument GetInfoPlist(string plistPath) {
// Get the plist file
PlistDocument plist = new PlistDocument();
plist.ReadFromFile(plistPath);
return plist;
}
internal static string GetProjectName(PlistDocument plist) {
string projectname = plist.root["CFBundleDisplayName"].AsString();
return projectname;
}
}
公共类HealthKitBuildProcessor:IPProcessSceneWithReport
{
私有静态字符串shareString=null;
私有静态字符串updateString=null;
私有静态字符串clinicalString=null;
/*!@IProcessScene界面所需的简短说明。设置为“高”可让其他后处理脚本先运行*/
公共int回拨订单{
获取{return 100;}
}
/*!@简短搜索HealthKitDataTypes对象并读取OnPostprocessBuild阶段的使用字符串。
@param scene正在处理的场景。
@param report包含有关当前生成的信息的报告
*/
公共void OnProcessScene(场景场景、构建报告){
GameObject[]根对象=场景。GetRootGameObjects();
foreach(根对象中的GameObject对象){
HealthKitDataTypes=obj.getComponentChildren();
if(类型!=null){
if(type.AskForSharePermission()){
HealthKitBuildProcessor.shareString=types.healthShareUsageDescription;
}
if(types.AskForUpdatePermission()){
HealthKitBuildProcessor.updateString=types.healthUpdateUsageDescription;
}
/*if(types.AskForClinicalPermission()){
HealthKitBuildProcessor.clinicalString=types.clinicalUsageDescription;
}*/
}
}
}
/*!@简短更新Xcode项目。
@param buildTarget是目标构建平台
@param path目标生成的路径
*/
[后处理属性(10)]
PostProcessBuild上的公共静态无效(BuildTarget BuildTarget,字符串路径){
Debug.Log(“--BEHEALTHKIT后处理构建--”;
if(buildTarget==buildTarget.iOS){
//字符串projPath=path+“/Unity iPhone.xcodeproj/project.pbxproj”;
//Log(“BE:PROJECT路径:”+projPath);
var projPath=PBXProject.GetPBXProjectPath(路径);
var proj=新PBX项目();
proj.ReadFromString(System.IO.File.ReadAllText(projPath));
#如果UNITY\u 2019\u 3\u或更新
字符串mainTarget=proj.GetUnityMainTargetGuid();
字符串frameworkTarget=proj.GetUnityFrameworkTargetGuid();
Debug.Log(“--BE:UNITY\u 2019\u 3\u或\u更新版本---”;
LogFormat(“主目标:{0}”,主目标);
LogFormat(“框架目标:{0}”,frameworkTarget);
#否则
字符串targetName=PBXProject.GetUnityTargetName();
字符串mainTarget=proj.TargetGuidByName(targetName);
Debug.Log(“--BE:ELSE UNITY\U 2019\U 3\U或\U REVERSE----”;
Log(“主目标:{0}”,mainTarget);
Log(“targetName:”,targetName);
#恩迪夫
bool addHealthRecordsCapability=(clinicalString!=null);
//Log(“addHealthRecordsCapability:,addHealthRecordsCapability”);
//Info.plist
//-----------
Log(“--BE:PLIST--”;
var info=ProcessInfoPList(路径,addHealthRecordsCapability);
//权利
//--------------
Log(“--BE:ProcessRights--”;
字符串权限相对=处理权限(路径、项目、主目标、信息、addHealthRecordsCapability);
#如果UNITY\u 2019\u 3\u或更新
//添加HealthKit功能
Log(“----projPath”+projPath);
ProjectCapabilityManager能力=新的ProjectCapabilityManager(项目路径,“权利.权利”,null,主目标);
AddHealthKit();
Debug.Log(“--BE:Capability UNITY\u 2019\u 3\u或\u更新版本---”;
//添加HealthKit框架
//项目AddFrameworkToProject(框架目标,“HealthKit.framework”,true);
//设置自定义链接标志
//项目AddBuildProperty(frameworkTarget,“其他标志”,“-ObjC”);
#否则
//添加HealthKit功能
Debug.Log(“----否则为:能力统一\u 2019\u 3\u或\u更新---”);
Log(“projectPath:+projPath”);
Log(“授权相对:+授权相对”);
Log(“targetName:+targetName”);
ProjectCapabilityManager capabilities=new ProjectCapabilityManager(项目路径、权利关系、目标名称);
AddHealthKit();
//添加HealthKit框架
项目AddFrameworkToProject(主目标,“HealthKit.framework”,正确);
//设置自定义链接标志
项目AddBuildProperty(主目标,“其他标志”、“-ObjC”);
#恩迪夫
项目写入文件(projPath);
}
}
// -------------------------------
内部静态PlistDocument ProcessInfoPList(字符串路径,bool addHealthRecordsCapability){
字符串plistPath=Path.Combine(路径,“Info.plist”);
PlistDocument info=GetInfoPlist(plistPath);
PlistElementDict rootDict=info.root;
////添加密钥
if(HealthKitBuildProcessor.shareString!=null){
rootDict.SetSt