借助“不再显示该广告”功能,用户可以关闭广告或停止显示广告,还可以标示出他们不感兴趣的广告。默认的非自定义版本如下所示:
借助 GADUnifiedNativeAd
,您可以设置自己的界面,让用户能够选择不再显示原生广告。具体操作步骤如下所示:
请求自定义版“不再显示该广告”
第一步是使用 GADNativeMuteThisAdLoaderOptions
类请求自定义版“不再显示该广告”功能。
Swift
adLoader = GADAdLoader(adUnitID: "[AD_UNIT_ID]", rootViewController: self, adTypes: [ .unifiedNative ], options: [GADNativeMuteThisAdLoaderOptions()]) adLoader.delegate = self adLoader.load(GADRequest())
Objective-C
GADNativeMuteThisAdLoaderOptions *muteOptions = [GADNativeMuteThisAdLoaderOptions new]; self.adLoader = [[GADAdLoader alloc] initWithAdUnitID:"[AD_UNIT_ID]" rootViewController:self adTypes:@[ kGADAdLoaderAdTypeUnifiedNative ] options:@[ muteOptions ]]; self.adLoader.delegate = self; [self.adLoader loadRequest:[GADRequest request]];
检查自定义版“不再显示该广告”是否可用
加载原生广告后,请检查原生广告的 isCustomMuteThisAdAvailable
属性。如果可用,请显示自定义按钮。
Swift
func adLoader(_ adLoader: GADAdLoader, didReceive nativeAd: GADUnifiedNativeAd){ nativeAdView.nativeAd = nativeAd if (nativeAd.isCustomMuteThisAdAvailable) { enableCustomMute(nativeAd.muteThisAdReasons) } else { hideCustomMute() } ... } func enableCustomMute(_ reasons:[GADMMuteThisAdReason]) { // TODO: This method should show your custom mute button and provide the list // of reasons to the interface that are to be displayed when the user mutes // the ad. } func hideCustomMute() { //TODO: Remove / hide the custom mute button from your user interface. }
Objective-C
- (void)adLoader:(GADAdLoader *)adLoader didReceiveUnifiedNativeAd: (GADUnifiedNativeAd *)nativeAd { self.nativeAdView.nativeAd = nativeAd; if (nativeAd.isCustomMuteThisAdAvailable) { [self enableCustomMuteWithReasons:nativeAd.muteThisAdReasons]; } else { [self hideCustomMute]; } } - (void)enableCustomMuteWithReasons:(NSArray<GADMuteThisAdReason *> *)reasons { // TODO: This method should show your custom mute button and provide the list // of reasons to the interface that are to be displayed when the user mutes the // ad. } - (void)hideCustomMute { // TODO: Remove / hide the custom mute button from your user interface. }
如何设置自定义版不再显示界面完全由您决定。您可以在广告上放置一个小小的关闭按钮,也可以提供其他界面供用户选择不再显示该广告。
显示“不再显示该广告”的原因
如果自定义版“不再显示该广告”可供使用,则原生广告的 muteThisAdReason
属性中将提供一组 GADMuteThisAdReason
对象。GADMuteThisAdReason 具有一个提供可显示字符串的 reasonDescription 属性。
我们建议您最好向用户展示这些原因,并让他们选择不再显示该广告的原因。 当用户点击其中一个原因时,您应该报告该广告不再显示并附上用户所选原因。
当用户点击关闭按钮时,您也可以选择不显示这些原因,而是直接报告用户执行了不再显示操作,但不提供具体原因。
不再显示该广告
不再显示该广告应该涉及两项操作:
- 报告不再显示原生广告的原因
- 在您的用户界面上,自行按照自己喜欢的方式不再显示/隐藏相应广告
Swift
func muteAdDialogDidSelect(reason: GADMuteThisAdReason) { // Report the mute action and reason to the ad. nativeAdView.nativeAd?.muteThisAd(with: reason) muteAd() } func muteAd() { // TODO: Mute / hide the ad in your preferred manner. }
Objective-C
- (void)muteAdDialogDidSelectReason:(GADMuteThisAdReason *)reason { // Report the mute action and reason to the ad. [self.nativeAdView.nativeAd muteThisAdWithReason:reason]; [self muteAd]; } - (void)muteAd { // TODO: Mute / hide the ad in your preferred manner. }
接收不再显示广告的确认通知(可选)
在报告不再显示的广告时,如果您希望收到报告已成功的通知,则可实现 GADUnifiedNativeAdDelegate
方法(nativeAdIsMuted:
)。只有在成功实现了不再显示相应广告后,才会调用此方法。