iOS项目加入iAD,Admob的简单代码

项目需要加广告,所以看了一下相关代码,很简单.

1.iAD 需要加入iad.framework

[objc]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:18px;">//.h  
  2.   
  3.   
  4.   
  5. #import <UIKit/UIKit.h>  
  6. #import <iAd/iAd.h>  
  7. @interface ViewController : UIViewController<ADBannerViewDelegate>  
  8.   
  9. @end</span>  

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



2.admob 

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



[objc]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:18px;">#define ADID @"xxxxxxx"  
  2. //设置一个自己的全局id</span>  

[objc]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:18px;">//.H  
  2. #import <UIKit/UIKit.h>  
  3. #import "GADBannerView.h"  
  4. @interface AdmobDefaultViewController : UIViewController  
  5. {  
  6.     GADBannerView *ADView;  
  7. }  
  8. @end</span>  

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



3.ADMOB插屏广告

[objc]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:18px;">  
  2.   
  3. #import <UIKit/UIKit.h>  
  4. #import "GADInterstitial.h"  
  5. #import "GADInterstitialDelegate.h"  
  6.   
  7. @interface InterAdmobViewController : UIViewController<GADInterstitialDelegate>  
  8.   
  9. @property(nonatomicretainGADInterstitial *interstitial;  
  10.   
  11. @end</span>  


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



参考链接:https://developers.google.com/mobile-ads-sdk/docs/admob/advanced?hl=zh-CN#iOS

源码:http://download.csdn.net/detail/woshiwls/6931237

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值