如何在Xamarin/MAUI中监听iOS App Open Ad的关闭事件?
在MAUI iOS中监听Google App Open Ad的关闭事件
你在MAUI中用Xamarin.Google.iOS.MobileAds包实现Google开屏广告时,想要监听广告关闭事件,对应iOS原生的GADFullScreenContentDelegate,可以按以下步骤实现:
步骤1:实现自定义的FullScreenContentDelegate
在C#中,需要创建一个继承自NSObject并实现IGADFullScreenContentDelegate接口的类,重写需要的回调方法:
public class AppOpenAdDelegate : NSObject, IGADFullScreenContentDelegate { // 广告成功展示时触发 public void AdDidPresentFullScreenContent(GADFullScreenContentAd ad) { // 处理广告展示逻辑 } // 广告关闭时触发(这就是你需要的关闭回调) public void AdDidDismissFullScreenContent(GADFullScreenContentAd ad) { // 这里写广告关闭后的逻辑,比如释放广告实例、重新加载下一次广告等 (ad as GADAppOpenAd)?.Dispose(); } // 广告展示失败时触发 public void AdDidFailToPresentFullScreenContent(GADFullScreenContentAd ad, NSError error) { // 处理展示失败逻辑 (ad as GADAppOpenAd)?.Dispose(); } }
步骤2:给AppOpenAd设置Delegate
加载广告后,将自定义的Delegate实例赋值给广告的FullScreenContentDelegate属性,替换你之前用PaidEventHandler的方式:
this.ad = await AppOpenAd.LoadAsync( o.IosUnitId, null, o.Orientation switch { AppOpenAdOrientation.Landscape => UIInterfaceOrientation.LandscapeLeft, _ => UIInterfaceOrientation.Portrait, } ); // 创建自定义Delegate实例 var adDelegate = new AppOpenAdDelegate(); // 绑定到广告的FullScreenContentDelegate this.ad.FullScreenContentDelegate = adDelegate; var root = Platform.GetCurrentUIViewController(); this.ad.PresentFromRootViewController(root);
补充说明
- 你之前用的
PaidEventHandler是监听广告付费事件的,和广告展示/关闭无关,不能用来判断广告是否打开或关闭。 - 对应Android端的
FullScreenContentCallback,iOS端就是通过GADFullScreenContentDelegate来实现所有全屏广告的展示、关闭、失败等回调。
内容的提问来源于stack exchange,提问作者Luke Vo
相关产品推荐
相关产品推荐

