Update GoogleSignInIOSBuildPatch.cs

iOS
Hazim Bin Ijaz 2 months ago
parent 0962b33d64
commit 88b265d6c9

@ -1,7 +1,5 @@
#if UNITY_IOS && UNITY_EDITOR #if UNITY_IOS && UNITY_EDITOR
using UnityEditor; using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEditor.Callbacks; using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode; using UnityEditor.iOS.Xcode;
using UnityEngine; using UnityEngine;
@ -12,7 +10,8 @@ public class GoogleSignInIOSBuildPatch
[PostProcessBuild(100)] [PostProcessBuild(100)]
public static void OnPostProcessBuild(BuildTarget buildTarget, string pathToBuiltProject) public static void OnPostProcessBuild(BuildTarget buildTarget, string pathToBuiltProject)
{ {
if (buildTarget != BuildTarget.iOS) return; if (buildTarget != BuildTarget.iOS)
return;
string plistPath = Path.Combine(pathToBuiltProject, "Info.plist"); string plistPath = Path.Combine(pathToBuiltProject, "Info.plist");
@ -21,20 +20,19 @@ public class GoogleSignInIOSBuildPatch
plist.ReadFromFile(plistPath); plist.ReadFromFile(plistPath);
PlistElementDict rootDict = plist.root; PlistElementDict rootDict = plist.root;
// 1. Set GIDClientID
rootDict.SetString("GIDClientID", GameConstants.iOS_ClientID); rootDict.SetString("GIDClientID", GameConstants.iOS_ClientID);
// 2. Add CFBundleURLSchemes
PlistElementArray urlTypes = rootDict.CreateArray("CFBundleURLTypes"); PlistElementArray urlTypes = rootDict.CreateArray("CFBundleURLTypes");
PlistElementDict urlDict = urlTypes.AddDict(); PlistElementDict urlDict = urlTypes.AddDict();
PlistElementArray schemes = urlDict.CreateArray("CFBundleURLSchemes"); PlistElementArray schemes = urlDict.CreateArray("CFBundleURLSchemes");
schemes.AddString(GameConstants.iOS_URL_Scheme); schemes.AddString(GameConstants.iOS_URL_Scheme);
plist.WriteToFile(plistPath); plist.WriteToFile(plistPath);
Debug.Log("✅ Info.plist patched with GIDClientID and CFBundleURLSchemes."); Debug.Log("✅ Patched Info.plist with GIDClientID and URL scheme.");
// === Modify UnityAppController.mm === // === Modify UnityAppController.mm ===
string appControllerPath = Path.Combine(pathToBuiltProject, "Classes", "UnityAppController.mm"); string appControllerPath = Path.Combine(pathToBuiltProject, "Classes", "UnityAppController.mm");
if (!File.Exists(appControllerPath)) if (!File.Exists(appControllerPath))
{ {
Debug.LogWarning("⚠️ UnityAppController.mm not found."); Debug.LogWarning("⚠️ UnityAppController.mm not found.");
@ -42,23 +40,37 @@ public class GoogleSignInIOSBuildPatch
} }
string content = File.ReadAllText(appControllerPath); string content = File.ReadAllText(appControllerPath);
string injection = $"[GIDSignIn sharedInstance].clientID = @\"{GameConstants.iOS_ClientID}\";";
// 1. Inject #import if missing
string importLine = "#import <GoogleSignIn/GoogleSignIn.h>";
if (!content.Contains(importLine))
{
int importInsertIndex = content.IndexOf("#import");
if (importInsertIndex >= 0)
{
int endOfLine = content.IndexOf("\n", importInsertIndex);
content = content.Insert(endOfLine + 1, importLine + "\n");
}
}
// 2. Inject GIDSignIn client ID line
string injection = $"[GIDSignIn sharedInstance].clientID = @\"{GameConstants.iOS_ClientID}\";";
if (!content.Contains(injection)) if (!content.Contains(injection))
{ {
string marker = "didFinishLaunchingWithOptions:(NSDictionary*)launchOptions"; string marker = "didFinishLaunchingWithOptions:(NSDictionary*)launchOptions";
int index = content.IndexOf(marker); int markerIndex = content.IndexOf(marker);
if (index > -1) if (markerIndex >= 0)
{ {
int braceIndex = content.IndexOf("{", index); int braceIndex = content.IndexOf("{", markerIndex);
if (braceIndex > -1) if (braceIndex >= 0)
{ {
content = content.Insert(braceIndex + 1, "\n " + injection); content = content.Insert(braceIndex + 1, "\n " + injection);
File.WriteAllText(appControllerPath, content);
Debug.Log("✅ UnityAppController.mm patched with GIDClientID.");
} }
} }
} }
File.WriteAllText(appControllerPath, content);
Debug.Log("✅ Patched UnityAppController.mm with GIDSignIn setup.");
} }
} }
#endif #endif

Loading…
Cancel
Save