iOS开发-植入广告(iAd, Admob实例)

一、iAd

1.需要加入iAd.framework

2.   .h文件加入如下代码,实现<ADBannerViewDelegate>协议

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #import <UIKit/UIKit.h>    
  2. #import <iAd/iAd.h>    
  3. @interface ViewController : UIViewController<ADBannerViewDelegate>   
3.   .m文件加入如下代码

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #import "ViewController.h"    
  2.     
  3. @interface ViewController ()    
  4. @property (nonatomic,strong)ADBannerView *adView;    
  5. @end    
  6.     
  7. @implementation ViewController    
  8.     
  9. - (void)viewDidLoad    
  10. {    
  11.     [super viewDidLoad];    
  12.     self.adView = [[ADBannerView alloc]initWithFrame:CGRectMake(0, 64, 320, 50)];    
  13.     self.adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifierPortrait];    
  14.     self.adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;    
  15.         
  16.     self.adView.delegate = self;    
  17.     [self.view addSubview:self.adView];    
  18. }    
  19.     
  20. - (void)bannerViewWillLoadAd:(ADBannerView *)banner{    
  21.     NSLog(@"bannerViewWillLoadAd");    
  22. }    
  23.     
  24. - (void)bannerViewDidLoadAd:(ADBannerView *)banner    
  25. {    
  26.     NSLog(@"bannerViewDidLoadAd");    
  27. }    
  28.     
  29. - (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error    
  30. {    
  31.     NSLog(@"didFailToReceiveAdWithError");    
  32. }    

效果图:



二、admob 

//google admob广告IOS初级指南

1.需要加入第三方文件,以及如下framework


2.

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #define ADID @"xxxxxxx"    
  2. //设置一个自己的全局id  

3.   .h文件加入如下代码
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #import <UIKit/UIKit.h>    
  2. #import "GADBannerView.h"    
  3. @interface AdmobDefaultViewController : UIViewController    
  4. {    
  5.     GADBannerView *ADView;    
  6. }  

3.   .m文件加入如下代码
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. - (void)viewDidLoad    
  2. {    
  3.     [super viewDidLoad];    
  4.     // Do any additional setup after loading the view, typically from a nib.    
  5.         
  6.     // Create a view of the standard size at the bottom of the screen.    
  7.     ADView = [[GADBannerView alloc]    
  8.                    initWithFrame:CGRectMake(0.0,self.view.frame.size.height - GAD_SIZE_320x50.height,GAD_SIZE_320x50.width,GAD_SIZE_320x50.height)];    
  9.         
  10.     ADView.adUnitID = ADID;//调用id    
  11.         
  12.     ADView.rootViewController = self;    
  13.     ADView.backgroundColor = [UIColor yellowColor];    
  14.     [self.view addSubview:ADView];    
  15.         
  16.     [ADView loadRequest:[GADRequest request]];    
  17. }  
效果图:

3、admob插屏广告


.h文件代码
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #import <UIKit/UIKit.h>    
  2. #import "GADInterstitial.h"    
  3. #import "GADInterstitialDelegate.h"    
  4.     
  5. @interface InterAdmobViewController : UIViewController<GADInterstitialDelegate>    
  6.     
  7. @property(nonatomic, retain) GADInterstitial *interstitial;    
  8.     
  9. @end  

.m文件代码
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. - (void)viewDidLoad    
  2. {    
  3.     [super viewDidLoad];    
  4.         
  5.     self.interstitial = [[GADInterstitial alloc] init];    
  6.     self.interstitial.delegate = self;    
  7.     self.interstitial.adUnitID = ADID;    
  8.         
  9.     [self.interstitial loadRequest: [self createRequest]];    
  10.         
  11. }    
  12.     
  13. - (GADRequest *)createRequest {    
  14.     GADRequest *request = [GADRequest request];    
  15.         
  16.     // Make the request for a test ad. Put in an identifier for the simulator as    
  17.     // well as any devices you want to receive test ads.    
  18.     request.testDevices =    
  19.     [NSArray arrayWithObjects:    
  20.      // TODO: Add your device/simulator test identifiers here. They are    
  21.      // printed to the console when the app is launched.    
  22.      nil nil];    
  23.     return request;    
  24. }    
  25.     
  26. - (void)interstitialDidReceiveAd:(GADInterstitial *)interstitial {    
  27.     [interstitial presentFromRootViewController:self];    
  28. }  
效果图:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值