今天在更新北京限行日历中的Admob时,pod update Google-Mobile-Ads-SDK后,build却报错了,在这里记录一下,方便有遇到同样错误的同学查看。
1、GADBannerViewDelegate方法变更,替换原有的代理方法。- (void)adViewDidReceiveAd:(GADBannerView *)bannerView已变更,需替换一下方法:
/// Tells the delegate that an ad request successfully received an ad. The delegate may want to add
/// the banner view to the view hierarchy if it hasn't been added yet.
- (void)bannerViewDidReceiveAd:(nonnull GADBannerView *)bannerView;
/// Tells the delegate that an ad request failed. The failure is normally due to network
/// connectivity or ad availablility (i.e., no fill).
- (void)bannerView:(nonnull GADBannerView *)bannerView
didFailToReceiveAdWithError:(nonnull NSError *)error;
/// Tells the delegate that an impression has been recorded for an ad.
- (void)bannerViewDidRecordImpression:(nonnull GADBannerView *)bannerView;
2、原有的kGADErrorInternalError改为GADErrorInternalError,kGADErrorTimeout改为GADErrorTimeout,其它错误code类似。
3、运行闪退,日志如下:
Exception NSException * “The Google Mobile Ads SDK was initialized without AppMeasurement. Google AdMob publishers, follow instructions here: https://googlemobileadssdk.page.link/admob-ios-update-plist to include the AppMeasurement framework and set the -ObjC linker flag. Google Ad Manager publishers, follow instructions here: https://googlemobileadssdk.page.link/ad-manager-ios-update-plist” 0x0000000280f7abe0
在Info.plist中添加GADIsAdManagerApp,值为YES。
参考链接:
Why “GADIsAdManagerApp” is required in the plist file since version 7.42?
https://groups.google.com/g/google-admob-ads-sdk/c/s_rkq9y1zvw?pli=1
4、增加Admob开屏广告
在AppDelegate的applicationDidBecomeActive回调方法中,增加方法调用:[self tryToPresentAd];
- (BOOL)needShowFullAd {
NSDate *now = [NSDate date];
NSTimeInterval timeIntervalBetweenNowAndLoadTime = [now timeIntervalSinceDate:self.loadTime];
double secondsPerHour = 60 * 20;
double intervalInHours = timeIntervalBetweenNowAndLoadTime / secondsPerHour;
return intervalInHours < n;
}
- (void)requestAppOpenAd {
self.appOpenAd = nil;
NSString *unitId = @"ca-app-pub-xxxxxxxxxxxxxxxxxxx";
#ifdef DEBUG
unitId = @"ca-app-pub-3940256099942544/5662855259";
#endif
[GADAppOpenAd loadWithAdUnitID:unitId
request:[GADRequest request]
orientation:UIInterfaceOrientationPortrait
completionHandler:^(GADAppOpenAd *_Nullable appOpenAd, NSError *_Nullable error) {
if (error) {
NSLog(@"Failed to load app open ad: %@", error);
return;
}
self.appOpenAd = appOpenAd;
self.appOpenAd.fullScreenContentDelegate = self;
self.loadTime = [NSDate date];
}];
}
#pragma mark - GADFullScreenContentDelegate
/// Tells the delegate that the ad failed to present full screen content.
- (void)ad:(nonnull id<GADFullScreenPresentingAd>)ad
didFailToPresentFullScreenContentWithError:(nonnull NSError *)error {
NSLog(@"didFailToPresentFullSCreenCContentWithError");
[self requestAppOpenAd];
}
/// Tells the delegate that the ad presented full screen content.
- (void)adDidPresentFullScreenContent:(nonnull id<GADFullScreenPresentingAd>)ad {
NSLog(@"adDidPresentFullScreenContent");
}
/// Tells the delegate that the ad dismissed full screen content.
- (void)adDidDismissFullScreenContent:(nonnull id<GADFullScreenPresentingAd>)ad {
NSLog(@"adDidDismissFullScreenContent");
[self requestAppOpenAd];
}
- (void)tryToPresentAd {
GADAppOpenAd *ad = self.appOpenAd;
self.appOpenAd = nil;
if (ad && [self needShowFullAd]) {
UIViewController *rootController = self.window.rootViewController;
[ad presentFromRootViewController:rootController];
} else {
// If you don't have an ad ready, request one.
[self requestAppOpenAd];
}
}
其中needShowFullAd方法用于判断距离上次开屏广告多久了,是否可以显示开屏广告。
具体接入指南可参考Admob文档:https://developers.google.com/admob/ios/app-open-ads?hl=zh-CN