LocalFacebookSdk.cs 5.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #if UNITY_EDITOR && UNITY_IOS
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using UnityEditor;
  5. using UnityEditor.Build;
  6. using UnityEditor.iOS.Xcode;
  7. using UnityEngine;
  8. internal static class LocalFacebookSdk
  9. {
  10. internal static void Configure(PBXProject project, string mainTarget, string frameworkTarget, string buildPath)
  11. {
  12. const string sdkRelativePath = "ThirdParty/FacebookSDK/Plugins/iOS/XCFrameworks";
  13. string sdkRoot = AppHelpPackagePaths.FullPath(sdkRelativePath);
  14. string[] modules =
  15. {
  16. "FBAEMKit", "FBSDKCoreKit_Basics", "FBSDKCoreKit",
  17. "FBSDKLoginKit", "FBSDKShareKit", "FBSDKGamingServicesKit"
  18. };
  19. foreach (string module in modules)
  20. {
  21. string assetPath = AppHelpPackagePaths.AssetPath(sdkRelativePath + "/" + module + ".xcframework");
  22. var importer = AssetImporter.GetAtPath(assetPath) as PluginImporter;
  23. if (importer == null || !importer.GetCompatibleWithPlatform(BuildTarget.iOS))
  24. throw new BuildFailedException("Local Facebook SDK must be enabled for iOS: " + assetPath);
  25. string xcframework = Path.Combine(sdkRoot, module + ".xcframework");
  26. if (!File.Exists(Path.Combine(xcframework, "Info.plist")))
  27. throw new BuildFailedException("Missing local Facebook XCFramework: " + xcframework);
  28. string manifest = Path.Combine(xcframework,
  29. "ios-arm64_arm64e/" + module + ".framework/PrivacyInfo.xcprivacy");
  30. if (!File.Exists(manifest))
  31. {
  32. if (module != "FBSDKGamingServicesKit")
  33. throw new BuildFailedException("Missing Facebook privacy manifest: " + manifest);
  34. continue;
  35. }
  36. string bundlePath = "FacebookSDKResources/" + module + "Privacy.bundle";
  37. string bundleDirectory = Path.Combine(buildPath, bundlePath);
  38. Directory.CreateDirectory(bundleDirectory);
  39. File.Copy(manifest, Path.Combine(bundleDirectory, "PrivacyInfo.xcprivacy"), true);
  40. var info = new PlistDocument();
  41. info.root.SetString("CFBundleIdentifier", "com.facebook.sdk." + module.Replace("_", "-") + ".privacy");
  42. info.root.SetString("CFBundleName", module + "Privacy");
  43. info.root.SetString("CFBundlePackageType", "BNDL");
  44. info.root.SetString("CFBundleVersion", "1");
  45. info.WriteToFile(Path.Combine(bundleDirectory, "Info.plist"));
  46. string bundleGuid = project.FindFileGuidByProjectPath(bundlePath);
  47. if (string.IsNullOrEmpty(bundleGuid))
  48. bundleGuid = project.AddFile(bundlePath, bundlePath, PBXSourceTree.Source);
  49. project.AddFileToBuild(mainTarget, bundleGuid);
  50. }
  51. foreach (string framework in new[]
  52. {
  53. "Security.framework", "SafariServices.framework", "AuthenticationServices.framework",
  54. "AdSupport.framework", "StoreKit.framework", "WebKit.framework", "Accelerate.framework"
  55. })
  56. project.AddFrameworkToProject(frameworkTarget, framework, false);
  57. project.AddFrameworkToProject(frameworkTarget, "AdServices.framework", true);
  58. foreach (string flag in new[] { "$(inherited)", "-ObjC", "-lz", "-lc++", "-lsqlite3" })
  59. project.AddBuildProperty(frameworkTarget, "OTHER_LDFLAGS", flag);
  60. project.SetBuildProperty(frameworkTarget, "CLANG_ENABLE_MODULES", "YES");
  61. project.SetBuildProperty(frameworkTarget, "SWIFT_VERSION", "5.0");
  62. foreach (string target in new[] { mainTarget, frameworkTarget })
  63. {
  64. project.AddBuildProperty(target, "LIBRARY_SEARCH_PATHS", "$(inherited)");
  65. project.AddBuildProperty(target, "LIBRARY_SEARCH_PATHS",
  66. "\"$(TOOLCHAIN_DIR)/usr/lib/swift/$(PLATFORM_NAME)\"");
  67. project.AddBuildProperty(target, "LIBRARY_SEARCH_PATHS", "\"$(SDKROOT)/usr/lib/swift\"");
  68. project.AddBuildProperty(target, "LD_RUNPATH_SEARCH_PATHS", "$(inherited)");
  69. project.AddBuildProperty(target, "LD_RUNPATH_SEARCH_PATHS", "/usr/lib/swift");
  70. project.AddBuildProperty(target, "LD_RUNPATH_SEARCH_PATHS", "@executable_path/Frameworks");
  71. }
  72. foreach (string sourcePath in project.GetRealPathsOfAllFiles(PBXSourceTree.Source))
  73. {
  74. if (Path.GetFileName(sourcePath) != "FacebookDynamicCredentials.m")
  75. continue;
  76. string bridgeGuid = project.FindFileGuidByRealPath(sourcePath, PBXSourceTree.Source);
  77. var flags = project.GetCompileFlagsForFile(frameworkTarget, bridgeGuid) ?? new List<string>();
  78. flags.Remove("-fno-objc-arc");
  79. if (!flags.Contains("-fobjc-arc"))
  80. flags.Add("-fobjc-arc");
  81. project.SetCompileFlagsForFile(frameworkTarget, bridgeGuid, flags);
  82. }
  83. }
  84. }
  85. #endif