Add iOS support for TapADN package

This commit is contained in:
2026-06-12 16:05:13 +08:00
parent 3341169f9b
commit 7e012bfd45
5 changed files with 621 additions and 72 deletions

View File

@@ -25,12 +25,17 @@ namespace Dirichlet.Mediation.Editor
/// </summary>
public class DirichletMediationIOSPostProcessor
{
private const string SDKVersion = "4.2.0.2";
private const string DefaultIOSSDKVersion = "4.2.0.1";
private const string MinIOSVersion = "11.0";
private const string DefaultTrackingUsageDescription = "该标识符将用于向您投放个性化广告";
// 环境变量 override仅在解析失败或接入方工程极端定制时使用
private const string ENV_FRAMEWORK_TARGET = "DIRICHLET_UNITY_FRAMEWORK_TARGET";
private const string ENV_APP_TARGET = "DIRICHLET_UNITY_APP_TARGET";
private const string ENV_IOS_SDK_VERSION = "DIRICHLET_IOS_SDK_VERSION";
private const string ENV_ATT_DESCRIPTION = "DIRICHLET_IOS_ATT_DESCRIPTION";
private const string PrefKeyIOSSDKVersion = "Dirichlet.iOS.SDKVersion";
private const string PrefKeyATTDescription = "Dirichlet.iOS.TrackingUsageDescription";
/// <summary>
/// 解析出的 target 信息
@@ -363,6 +368,7 @@ namespace Dirichlet.Mediation.Editor
// Note: DirichletAdSDK (DRA adapter) is always enabled as core SDK
var enableCsj = EditorPrefs.GetBool("Dirichlet.iOS.EnableCSJ", true);
var enableGdt = EditorPrefs.GetBool("Dirichlet.iOS.EnableGDT", true);
var sdkVersion = ResolveIOSSDKVersion();
var podfileContent = new StringBuilder();
podfileContent.AppendLine("# Generated by Dirichlet Mediation Unity Plugin");
@@ -379,20 +385,20 @@ namespace Dirichlet.Mediation.Editor
// 所有 pods 统一放到 Framework target
// SDK 通过 NSClassFromString 查找 Adapter 类,必须在同一 target 中
podfileContent.AppendLine($"target '{targetInfo.FrameworkTargetName}' do");
podfileContent.AppendLine($" pod 'DirichletMediationSDK', '{SDKVersion}'");
podfileContent.AppendLine($" pod 'DirichletMediationSDK', '{sdkVersion}'");
if (enableCsj)
{
podfileContent.AppendLine($" pod 'DirichletMediationAdapterCSJ', '{SDKVersion}'");
podfileContent.AppendLine($" pod 'DirichletMediationAdapterCSJ', '{sdkVersion}'");
}
if (enableGdt)
{
podfileContent.AppendLine($" pod 'DirichletMediationAdapterGDT', '{SDKVersion}'");
podfileContent.AppendLine($" pod 'DirichletMediationAdapterGDT', '{sdkVersion}'");
}
// DirichletAdSDK (DRA adapter) is always included as core SDK
podfileContent.AppendLine($" pod 'DirichletMediationAdapterDRA', '{SDKVersion}'");
podfileContent.AppendLine($" pod 'DirichletMediationAdapterDRA', '{sdkVersion}'");
podfileContent.AppendLine("end");
podfileContent.AppendLine();
@@ -411,7 +417,24 @@ namespace Dirichlet.Mediation.Editor
File.WriteAllText(podfilePath, podfileContent.ToString());
Debug.Log($"[DirichletMediation] Generated Podfile at {podfilePath}");
Debug.Log($"[DirichletMediation] All pods allocated to {targetInfo.FrameworkTargetName} (CSJ={enableCsj}, GDT={enableGdt}, DRA=always)");
Debug.Log($"[DirichletMediation] All pods allocated to {targetInfo.FrameworkTargetName} (version={sdkVersion}, CSJ={enableCsj}, GDT={enableGdt}, DRA=always)");
}
private static string ResolveIOSSDKVersion()
{
var envVersion = System.Environment.GetEnvironmentVariable(ENV_IOS_SDK_VERSION);
if (!string.IsNullOrWhiteSpace(envVersion))
{
return envVersion.Trim();
}
var prefVersion = EditorPrefs.GetString(PrefKeyIOSSDKVersion, string.Empty);
if (!string.IsNullOrWhiteSpace(prefVersion))
{
return prefVersion.Trim();
}
return DefaultIOSSDKVersion;
}
/// <summary>
@@ -466,13 +489,15 @@ namespace Dirichlet.Mediation.Editor
Debug.Log($" Framework target: {targetInfo.FrameworkTargetName} (GUID: {targetGuid})");
Debug.Log($" App target: {targetInfo.AppTargetName} (GUID: {mainTargetGuid})");
// NOTE: System frameworks (AdSupport, AVFoundation, WebKit, CoreVideo, etc.)
// NOTE: Most system frameworks (AdSupport, AVFoundation, WebKit, CoreVideo, etc.)
// are declared in SDK podspecs and will be automatically linked by CocoaPods.
// No need to manually add them here.
// AppTrackingTransparency is referenced by the Unity bridge, so add it explicitly and weak-link it for iOS 11+.
// - DirichletAdSDK.podspec: AdSupport, SystemConfiguration, Security
// - DirichletCoreSDK.podspec: SystemConfiguration, Security
// - DirichletMediationAdapterCSJ.podspec: CoreVideo
// - Third-party SDKs (Ads-CN, GDTMobSDK) declare their own framework dependencies.
pbxProject.AddFrameworkToProject(targetGuid, "AppTrackingTransparency.framework", true);
pbxProject.AddFrameworkToProject(targetGuid, "AdSupport.framework", true);
// Set build settings for framework target
pbxProject.SetBuildProperty(targetGuid, "ENABLE_BITCODE", "NO");
@@ -591,16 +616,16 @@ namespace Dirichlet.Mediation.Editor
// Note: The following Info.plist keys should be configured by the developer manually
// to avoid potential App Store review issues:
// - NSAppTransportSecurity: Configure based on your app's network requirements
// - NSUserTrackingUsageDescription: Required for iOS 14+ IDFA access, use your custom description
// - NSLocationWhenInUseUsageDescription: Only add if your app uses location services
//
// Reference: https://ssp.dirichlet.cn/docs/dirichlet-mediation-sdk/dirichlet-mediation-sdk-guide-ios/
// Add SKAdNetwork identifiers for attribution tracking
AddSKAdNetworkIds(rootDict);
AddTrackingUsageDescription(rootDict);
plist.WriteToFile(plistPath);
Debug.Log("[DirichletMediation] Modified Info.plist (SKAdNetwork IDs only)");
Debug.Log("[DirichletMediation] Modified Info.plist (SKAdNetwork IDs + ATT description)");
}
private static void AddSKAdNetworkIds(PlistElementDict rootDict)
@@ -631,6 +656,35 @@ namespace Dirichlet.Mediation.Editor
Debug.Log($"[DirichletMediation] Added {commonSkAdNetworkIds.Length} SKAdNetwork IDs to Info.plist");
}
private static void AddTrackingUsageDescription(PlistElementDict rootDict)
{
if (rootDict.values.ContainsKey("NSUserTrackingUsageDescription"))
{
Debug.Log("[DirichletMediation] NSUserTrackingUsageDescription already exists, skipping");
return;
}
rootDict.SetString("NSUserTrackingUsageDescription", ResolveTrackingUsageDescription());
Debug.Log("[DirichletMediation] Added NSUserTrackingUsageDescription to Info.plist");
}
private static string ResolveTrackingUsageDescription()
{
var envDescription = System.Environment.GetEnvironmentVariable(ENV_ATT_DESCRIPTION);
if (!string.IsNullOrWhiteSpace(envDescription))
{
return envDescription.Trim();
}
var prefDescription = EditorPrefs.GetString(PrefKeyATTDescription, string.Empty);
if (!string.IsNullOrWhiteSpace(prefDescription))
{
return prefDescription.Trim();
}
return DefaultTrackingUsageDescription;
}
private static void RunPodInstall(string projectPath)
{
var podfilePath = Path.Combine(projectPath, "Podfile");