iOS应用添加Google广告

关于广告(5步)
google admob广告
第一要注册一个google ID for self.adBanner.adUnitID
创建ID具体操作介绍:

进apps.admob.com注册admob账号:账号由AdSense账号和AdWords账号组成
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

第三是引入这个framework到工程中
然后引入头文件#import 到viewcontroller上

第四 见代码:
示例代码github地址:https://github.com/googleads/googleads-mobile-ios-examples
官方说明文档:https://developers.google.com/mobile-ads-sdk/docs/admob/ios/quick-start#_adc=ww-zh-Hans-et-HC

一般会在table的footer上加上这个广告
@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”;//在这个时间之前的就显示广告,否则不显示

    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];
    

    }

}

第五实现两个delegate方法,在请求google广告之后的成功&失败回调

  • (void)adViewDidReceiveAd:(GADBannerView *)view {

    self.adBanner.hidden = NO;

}

  • (void)adView:(GADBannerView *)view didFailToReceiveAdWithError:(GADRequestError *)error {

    self.adBanner = nil;

    self.table.tableFooterView = nil;

}

你可能感兴趣的:(iOS应用添加Google广告)