?
本范例只著重在如何使用 ADBannerView 并沒有 iAD 的相關設定與申請流程,至于其他行動廣告的部份大家可以參考 行動廣告 Banner!讓你賺大錢 !一文。
?
?首先匯入 iAD Framework,并引用其標頭檔,Xcode 4 Framework 匯入方式可以參考 Xcode 4 新增 Framework 的方法 一文。
?
?
#import <iAd/iAd.h>?
?接著在要使用此物件的類別上設定 ?代理,并建立一個 ADBannerView 型態的物件。
@interface AdBannerViewViewController : UIViewController { ADBannerView *bannerView; }??
?在功能方面,我們希望 ADBannerView 能夠與畫面的呈現方式一致(直立與傾置),并且在 ADBannerView 準備好時能自動彈出顯示,使用者也能自行切換是否要顯示此 ADBannerView。
?
?針對上述這些功能對 ADBannerView 進行初始化的動作,其程式碼如下。
//自定義的函式 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 移動到畫面內,制作出類似彈出的效果。
- (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 準備好時會自動彈出來,以及使用者也可以自行切換它,所以必須在下列兩個事件中呼叫上述函式。
//使用者按鈕事件 - (IBAction)onButtonPress:(id)sender { [self bannerViewAnimation]; } //當ADBannerView完成讀取廣告時會觸發的事件 - (void)bannerViewDidLoadAd:(ADBannerView *)banner { [self bannerViewAnimation]; }?
?現在還剩下最后一個功能,就是 ADBannerView 能夠與畫面的呈現方式一致(直立與傾置),我們可以使用下列函式來重新設定 ADBannerView 的位置與他的類型。
//畫面轉動時呼叫的內建函式 - (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 初始化函式,并在對應的函式中釋放其記憶體。
- (void)viewDidLoad { [super viewDidLoad]; [self initializeBanner]; }?
- (void)dealloc { [bannerView release]; [super dealloc]; }?
?另外下面提供幾個跟 ADBannerView 有關的內建函式。
//ADBannerView發生錯誤時會觸發 - (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {}??
//按下廣告時會觸發的函式 -(BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave { //這里可以放置暫停你應用程式的程式碼 return YES; }??
//重廣告中按下返回應用程式按鈕時所觸發的函式 - (void)bannerViewActionDidFinish:(ADBannerView *)banner { //這里可以放置重啟你應用程式的程式碼 }?
?ps:在測試 ADBannerView 必須要連上網際網路才行,有時候測試用的 iAD 會因為連線煩繁忙等問題無法取得測試用廣告,可能需要多執行幾次。
?
?
?
來源:
http://furnacedigital.blogspot.com/2011/09/adbannerview.html
?
參考:
?
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
