iOS Maui集成UMP SDK报错:无法找到Google.MobileAds.Consent相关类
问题:Xamarin iOS集成UMP SDK时找不到
Google.MobileAds.Consent命名空间及ConsentInformation 因AdMob要求需集成UMP SDK,Android端已成功完成集成,但iOS端遇到问题。参考相关讨论后,编译器仍提示无法找到using Google.MobileAds.Consent;及ConsentInformation。已安装以下NuGet包:
- Xamarin.Google.iOS.UserMessagingPlatform
- Xamarin.Google.iOS.MobileAds
- Xamarin.Google.UserMessagingPlatform
相关代码如下:
using Foundation; using Google.MobileAds; using UIKit; using Google.MobileAds.Consent; namespace Orbis.Mobile; [Register("AppDelegate")] public class AppDelegate : MauiUIApplicationDelegate { protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp(); public override bool OpenUrl(UIApplication app, NSUrl url, NSDictionary options) { AuthenticationContinuationHelper.SetAuthenticationContinuationEventArgs(url); return base.OpenUrl(app, url, options); } public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions) { MobileAds.SharedInstance.Start(CompletionHandler); //MobileAds.Configure("<YOUR_APP_ID>"); ConsentInformation.SharedInstance.RequestConsentInfoUpdate(new string[] { "pub-056789012345" }, (error) => { if (error == null) { if (ConsentInformation.SharedInstance.IsRequestLocationInEeaOrUnknown) { ConsentForm consentForm = new ConsentForm(); consentForm.Load((loadError) => { if (loadError == null) { consentForm.Show((dismissError, userPrefersAdFree) => { if (dismissError == null) { switch (ConsentInformation.SharedInstance.ConsentStatus) { case ConsentStatus.NonPersonalized: break; case ConsentStatus.Personalized: break; default: break; } } }); } }); } } }); return base.FinishedLaunching(application, launchOptions); } private void CompletionHandler(InitializationStatus status) { } }
解决方案
1. 修正命名空间引用
Xamarin iOS的UMP SDK中,ConsentInformation等相关类不在Google.MobileAds.Consent命名空间下,而是直接位于Google.UserMessagingPlatform命名空间。把代码中的引用替换为:
using Google.UserMessagingPlatform;
2. 适配UMP SDK的API调用方式
你当前的代码是旧版Consent SDK的写法,新版本UMP SDK的API已更新,需替换为以下标准调用逻辑:
// 替换原有的ConsentInformation相关代码 var parameters = new UMPRequestParameters(); parameters.TagForUnderAgeOfConsent = false; // 根据应用实际受众设置 UMPConsentInformation.SharedInstance.RequestConsentInfoUpdate(parameters, (error) => { if (error == null) { if (UMPConsentInformation.SharedInstance.ConsentStatus == UMPConsentStatus.Required) { // 加载并展示授权表单 UMPConsentForm.Load((form, loadError) => { if (loadError == null && form != null) { form.PresentFromViewController(this, (dismissError) => { if (dismissError == null) { // 处理用户选择后的逻辑 switch (UMPConsentInformation.SharedInstance.ConsentStatus) { case UMPConsentStatus.NonPersonalized: // 非个性化广告逻辑 break; case UMPConsentStatus.Personalized: // 个性化广告逻辑 break; default: break; } } }); } }); } } });
3. 清理并重新安装NuGet包
- 移除所有相关的UMP/MobileAds NuGet包,手动清理项目的
bin和obj文件夹 - 仅重新安装
Xamarin.Google.iOS.UserMessagingPlatform和Xamarin.Google.iOS.MobileAds(Xamarin.Google.UserMessagingPlatform是Android端专属包,无需在iOS项目中安装) - 确保安装的包版本为最新稳定版,且版本号相互匹配
4. 验证项目基础配置
- 确认iOS项目的目标系统版本不低于UMP SDK要求的iOS 11.0
- 在
Info.plist中添加网络权限描述(如NSAppTransportSecurity相关配置),确保SDK能正常联网请求授权信息
内容的提问来源于stack exchange,提问作者Chris Curt
相关产品推荐
相关产品推荐

