iOS應(yīng)用添加Google廣告

關(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

http://www.google.cn/ads/admob/index.html?_adc=ap-zh_CN-ha-bk_zh_CN&medium=ha&gclid=CJr10omkjMQCFQ8GvAodB2IAWg

第二要下載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;

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容