iOS開發-植入廣告(iAd, Admob執行個體)

來源:互聯網
上載者:User

標籤:style   blog   class   code   tar   ext   

應用中植入廣告是一種很好的盈利手段。

下面介紹主流的兩種方法。iAd, Admob


先mark一個很詳細的pdf。   http://pan.baidu.com/share/link?shareid=1656439633&uk=1394536315&fid=406566606116897

一。iAd

1.需要加入iAd.framework

2.   .h檔案加入如下代碼

#import <UIKit/UIKit.h>  #import <iAd/iAd.h>  @interface ViewController : UIViewController<ADBannerViewDelegate> 

3.   .m檔案加入如下代碼


#import "ViewController.h"    @interface ViewController ()  @property (nonatomic,strong)ADBannerView *adView;  @end    @implementation ViewController    - (void)viewDidLoad  {      [super viewDidLoad];      self.adView = [[ADBannerView alloc]initWithFrame:CGRectMake(0, 64, 320, 50)];      self.adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifierPortrait];      self.adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;            self.adView.delegate = self;      [self.view addSubview:self.adView];  }    - (void)bannerViewWillLoadAd:(ADBannerView *)banner{      NSLog(@"bannerViewWillLoadAd");  }    - (void)bannerViewDidLoadAd:(ADBannerView *)banner  {      NSLog(@"bannerViewDidLoadAd");  }    - (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error  {      NSLog(@"didFailToReceiveAdWithError");  }  

:







二。admob 

1.需要加入第三方檔案,以及如下framework


2.

#define ADID @"xxxxxxx"  //設定一個自己的全域id

3.   .h檔案加入如下代碼
#import <UIKit/UIKit.h>  #import "GADBannerView.h"  @interface AdmobDefaultViewController : UIViewController  {      GADBannerView *ADView;  }

3.   .m檔案加入如下代碼
- (void)viewDidLoad  {      [super viewDidLoad];      // Do any additional setup after loading the view, typically from a nib.            // Create a view of the standard size at the bottom of the screen.      ADView = [[GADBannerView alloc]                     initWithFrame:CGRectMake(0.0,self.view.frame.size.height - GAD_SIZE_320x50.height,GAD_SIZE_320x50.width,GAD_SIZE_320x50.height)];            ADView.adUnitID = ADID;//調用id            ADView.rootViewController = self;      ADView.backgroundColor = [UIColor yellowColor];      [self.view addSubview:ADView];            [ADView loadRequest:[GADRequest request]];  }




3。ADMOB插屏廣告
.h檔案代碼
#import <UIKit/UIKit.h>  #import "GADInterstitial.h"  #import "GADInterstitialDelegate.h"    @interface InterAdmobViewController : UIViewController<GADInterstitialDelegate>    @property(nonatomic, retain) GADInterstitial *interstitial;    @end

.m檔案代碼
- (void)viewDidLoad  {      [super viewDidLoad];            self.interstitial = [[GADInterstitial alloc] init];      self.interstitial.delegate = self;      self.interstitial.adUnitID = ADID;            [self.interstitial loadRequest: [self createRequest]];        }    - (GADRequest *)createRequest {      GADRequest *request = [GADRequest request];            // Make the request for a test ad. Put in an identifier for the simulator as      // well as any devices you want to receive test ads.      request.testDevices =      [NSArray arrayWithObjects:       // TODO: Add your device/simulator test identifiers here. They are       // printed to the console when the app is launched.       nil nil];      return request;  }    - (void)interstitialDidReceiveAd:(GADInterstitial *)interstitial {      [interstitial presentFromRootViewController:self];  }



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.