關(guān)于廣告(5步)
google admob廣告
第一要注冊(cè)一個(gè)google ID for self.adBanner.adUnitID
創(chuàng)建ID具體操作介紹:
進(jìn)apps.admob.com注冊(cè)admob賬號(hào):賬號(hào)由AdSense賬號(hào)和AdWords賬號(hào)組成
https://support.google.com/admob/v2/answer/3052638
第二要下載adk
https://developers.google.com/mobile-ads-sdk/docs/admob/ios/download
第三是引入這個(gè)framework到工程中
然后引入頭文件#import <GoogleMobileAds/GADBannerView.h>到viewcontroller上
第四 見代碼:
示例代碼github地址:https://github.com/googleads/googleads-mobile-ios-examples
官方說(shuō)明文檔:https://developers.google.com/mobile-ads-sdk/docs/admob/ios/quick-start#_adc=ww-zh-Hans-et-HC
一般會(huì)在table的footer上加上這個(gè)廣告
@property (nonatomic, strong) GADBannerView *adBanner;
-
(void)resetTableFootView {
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
[self showiAdViewIfNeed:view];
self.table.tableFooterView = view;
}
-
(void)showiAdViewIfNeed:(UIView *)containerView {
if (containerView == NULL) {
return;}
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *dateString =@"2019-04-01 00:00:00”;//在這個(gè)時(shí)間之前的就顯示廣告,否則不顯示
NSDate *validDate = [dateFormatter dateFromString:dateString];
BOOL isHideiAd = [DateUtil dateCalendarBeforeToday:validDate];
if (!isHideiAd) {
CGPoint origin = CGPointMake(0.0, 0.0); self.adBanner = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner origin:origin]; self.adBanner.adUnitID = @"ca-app-pub-5056085843442632/2662170106"; self.adBanner.delegate = self;//GADBannerViewDelegate [self.adBanner setRootViewController:self]; [self.view addSubview:self.adBanner]; [self.adBanner loadRequest: [GADRequest request]]; containerView.frame = CGRectMake(containerView.frame.origin.x, containerView.frame.origin.y, containerView.frame.size.width, containerView.frame.size.height + self.adBanner.frame.size.height+5); [containerView addSubview:self.adBanner];}
}
第五實(shí)現(xiàn)兩個(gè)delegate方法,在請(qǐng)求google廣告之后的成功&失敗回調(diào)
-
(void)adViewDidReceiveAd:(GADBannerView *)view {
self.adBanner.hidden = NO;
}
-
(void)adView:(GADBannerView *)view didFailToReceiveAdWithError:(GADRequestError *)error {
self.adBanner = nil;
self.table.tableFooterView = nil;
}