ADBannerView 的基本使用方法

來源:互聯網
上載者:User

本範例只著重在如何使用 ADBannerView 並沒有 iAD 的相關設定與申請流程,至於其他行動廣告的部份大家可以參考行動廣告 Banner!讓你賺大錢!一文。  首先匯入 iAD Framework,並引用其標題檔,Xcode 4 Framework 匯入方式可以參考 Xcode 4 新增 Framework 的方法一文。   C代碼  #import <iAd/iAd.h>    接著在要使用此物件的類別上設定  代理,並建立一個 ADBannerView 型態的物件。C代碼  @interface AdBannerViewViewController : UIViewController  {  ADBannerView *bannerView;  }     在功能方面,我們希望 ADBannerView 能夠與畫面的呈現方式一致(直立與傾置),並且在 ADBannerView 準備好時能自動彈出顯示,使用者也能自行切換是否要顯示此 ADBannerView。  針對上述這些功能對 ADBannerView 進行初始化的動作,其程式碼如下。C代碼  //自訂的函式 Banner初始化(以畫面直立)  - (void)initializeBanner {    //以畫面直立的方式設定Banner於畫面底部  bannerView = [[ADBannerView alloc]initWithFrame:CGRectMake(0.0, 430.0, self.view.frame.size.width, 50.0)];    //此Banner所能支援的類型  bannerView.requiredContentSizeIdentifiers = [NSSet setWithObjects:ADBannerContentSizeIdentifierPortrait, ADBannerContentSizeIdentifierLandscape, nil];    //目前的Banner 類型  bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;    //設定代理  bannerView.delegate = self;    //無法按下觸發廣告  bannerView.userInteractionEnabled = NO;    //設定偏位移量  bannerView.frame = CGRectOffset(bannerView.frame, 0, 50);    [self.view addSubview:bannerView];  }    現在我們有了一個 ADBannerView,不過他的目前因為位移量的關係在畫面之外,暫時無法看到,所以下面程式碼使用動畫的方式將 ADBannerView 移動到畫面內,製作出類似彈出的效果。C代碼  - (void)bannerViewAnimation {    //動畫設定  [UIView beginAnimations:@"BannerViewAnimation" context:NULL];    //以userInteractionEnabled狀態排判斷bannerView是否在畫面內  if (bannerView.userInteractionEnabled) {  bannerView.frame = CGRectOffset(bannerView.frame, 0, 50);  }   else {  bannerView.frame = CGRectOffset(bannerView.frame, 0, -50);  }    //開始動畫  [UIView commitAnimations];    //將userInteractionEnabled做反向設定  bannerView.userInteractionEnabled = !bannerView.userInteractionEnabled;  }    上述程式碼使用 userInteractionEnabled 這個參數來當作判斷 ADBannerView 是否存在於畫面內的依據,並且使用類似開關的技巧,每一次執行該函式都會得到一個反向的結果。由於我們希望 ADBannerView 準備好時會自動彈出來,以及使用者也可以自行切換它,所以必須在下列兩個事件中呼叫上述函式。C代碼  //使用者按鈕事件  - (IBAction)onButtonPress:(id)sender {  [self bannerViewAnimation];  }    //當ADBannerView完成讀取廣告時會觸發的事件  - (void)bannerViewDidLoadAd:(ADBannerView *)banner {  [self bannerViewAnimation];  }    現在還剩下最後一個功能,就是 ADBannerView 能夠與畫面的呈現方式一致(直立與傾置),我們可以使用下列函式來重新設定 ADBannerView 的位置與他的類型。C代碼  //畫面轉動時呼叫的內建函式  - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {    //還原bannerView預設值  bannerView.userInteractionEnabled = NO;  bannerView.frame = CGRectOffset(bannerView.frame, 0,0);    //判斷畫面是否傾置  if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {  bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape;  bannerView.frame = CGRectMake(0.0, 288.0, self.view.frame.size.width, 32.0);  }  else {  bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;  bannerView.frame = CGRectMake(0.0, 430.0, self.view.frame.size.width, 50.0);  }    //重新設定位移量  bannerView.frame = CGRectOffset(bannerView.frame, 0, 50);  }    最後,不要忘記在程式進入點引用 ADBannerView 初始化函式,並在對應的函式中釋放其記憶體。C代碼  - (void)viewDidLoad  {  [super viewDidLoad];  [self initializeBanner];  }   C代碼  - (void)dealloc  {  [bannerView release];  [super dealloc];  }    另外下面提供幾個跟 ADBannerView 有關的內建函式。C代碼  //ADBannerView發生錯誤時會觸發  - (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {}    C代碼  //按下廣告時會觸發的函式  -(BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave {     //這裡可以放置暫停你應用程式的程式碼  return YES;  }    C代碼  //重廣告中按下返回應用程式按鈕時所觸發的函式  - (void)bannerViewActionDidFinish:(ADBannerView *)banner {  //這裡可以放置重啟你應用程式的程式碼  }    ps:在測試 ADBannerView 必須要連上網際網路才行,有時候測試用的 iAD 會因為連線煩繁忙等問題無法取得測試用廣告,可能需要多執行幾次。  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.