Smart Banner
於 2018年6月21日 (四) 03:41 由 imported>Wikiuser 所做的修訂
智慧型橫幅廣告只占螢幕的一小部分,使用者點擊後可以有全螢幕的瀏覽體驗,簡單的建立方式讓開發者輕鬆置入廣告
加入 TADBannerView
TADBannerView 物件是 UIView 的 subclass, 呈現html5橫幅廣告 加入橫幅廣告的簡單七步驟, 建議在viewController裡執行
- 匯入 #import <TAMediaAdsFramework/TAMediaAdsFramework.h>
- 在 UIViewController裡宣告 TADBannerView 變數
- 建立橫幅廣告
- 設定Unit ID
- 設定 rootViewController
- 將橫幅廣告加入UI
- 請求廣告
// BannerViewController.h // 匯入 TAMedia SDK 定義 #import <TAMediaAdsFramework/TAMediaAdsFramework.h> @interface BannerViewController : UIViewController <TADBannerViewDelegate> { // 以 instant variable 的方式, 宣告 bannerView 物件 TADBannerView *bannerView; } @end
// BannerViewController.m - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor whiteColor]; self.title = @"TAMedia Banner"; UIButton *fireButton1 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; fireButton1.frame = CGRectMake(10, 120, 200, 40); fireButton1.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:239/255.0 alpha:1.0]; [fireButton1 setTitle:@"Banner Ad" forState:UIControlStateNormal]; [fireButton1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [fireButton1 addTarget:self action:@selector(requestAd1) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:fireButton1]; } - (void)requestAd1 { TADRequest *request = [TADRequest request]; // Type: TADGender request.gender = [_GENDER_]; // Set birthday [request setBirthdayWithYear:[_YEAR_] month:[_MONTH_] day:[_DAY_]]; // Set location [request setLocationWithLatitude:[_LATITUDE_] longitude:[_LONGITUDE_] accuracy:[_ACCURACY_]]; // bannerView 物件初始化, 帶入自訂的origin CGFloat hOffset; if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone ) { hOffset = 50.f; } else { hOffset = 90.f; } if (!bannerView) { bannerView = [[TADBannerView alloc] initWithAdSize:kTADAdSizeSmartBannerPortrait origin:CGPointMake(0, self.view.frame.size.height - hOffset)]; } // 必須要設定delegate bannerView.delegate = self; // 設定 AD Unit ID bannerView.adUnitID = [_YOUR_AD_UNIT_ID_]; // 設定 rootViewController bannerView.rootViewController = self; // 載入廣告 [bannerView loadRequest:request]; }
特別注意
// BannerViewController.m - (void)viewWillDisappear:(BOOL)animated { // viewController 的 view 消失前, 必須將 bannerView 及其 delegate 設為 nil if (bannerView != nil) { bannerView.delegate = nil; bannerView = nil; } }
TADBannerViewDelegate
@protocol TADBannerViewDelegate <NSObject> @optional // Ad Request Lifecycle Notifications // 廣告載入成功後被呼叫, 可以在這裡呈現廣告 - (void)adViewDidReceiveAd:(TADBannerView *)view; // 廣告載入失敗後被呼叫 - (void)adView:(TADBannerView *)view didFailToReceiveAdWithError:(TADRequestError *)error; // Click-Time Lifecycle Notifications // 廣告即將被呈現之前被呼叫 - (void)adViewWillPresentScreen:(TADBannerView *)adView; // 廣告即將被關閉之前被呼叫 - (void)adViewWillDismissScreen:(TADBannerView *)adView; // 廣告被關閉之後被呼叫 - (void)adViewDidDismissScreen:(TADBannerView *)adView; // 應用程式及將改在背景執行或中止運作前被呼叫 - (void)adViewWillLeaveApplication:(TADBannerView *)adView; @end