SDK8 Banner
簡介
橫幅廣告只占螢幕的一小部分,使用者點擊後可以進入廣告頁面進行瀏覽,簡單的建立方式讓開發者輕鬆置入廣告。
串接準備
開始串接前請間確定已將 TAmedia SDK 整合至專案,並且完成初始化 SDK步驟,若您尚未完成請先參考開始使用教學完成相關設定。
Reference document
Banners 大小種類
BannerDefault | SmartBanner | MediumRectangle | LargeRectangle |
---|---|---|---|
320x50 | portrait: full widthx50(phone), full widthx90(pad) lanscape: full widthx32(phone), full widthx90(pad) |
320x250 | 1200x627 |
加入 TADBannerView
TADBannerView 物件是 UIView 的 subclass, 呈現html5橫幅廣告 加入橫幅廣告的簡單七步驟, 建議在viewController裡執行
- 匯入 TAMediaAdsFramework
- 在 UIViewController裡宣告 TADBannerView 變數
- 建立橫幅廣告,AD Size
- 設定Unit ID
- 設定 rootViewController
- 請求廣告
BannerViewController
// 匯入 TAMedia SDK 定義 import TAMediaAdsFramework @objc enum BannerType: Int { case BannerDefault case Banner320x250 case Banner1200x627 case SmartBanner } class BannerViewController: : UIViewController { // 宣告 TADBannerView 變數 var banner: TADBannerView? requestAd() override func viewDidLoad() { super.viewDidLoad() requestAd() } func requestAd() { let request = TADRequest() request.showLog = true request.setLocationWithLatitude(25.02246916213678, longitude: 121.54834205241194, accuracy: 1) // 依Banner種類設定AD Size if bannerType == .BannerDefault { banner = TADBannerView.init(adSize: kTADAdSizeBanner, origin: CGPoint(x: 10, y: self.view.frame.size.height - 100)) banner?.adUnitID = Constants.BannerAd320x50SlotID } else if bannerType == .Banner320x250 { banner = TADBannerView.init(adSize: kTADAdSizeMediumRectangle, origin: CGPoint(x: 10, y: self.view.frame.size.height - 300)) banner?.adUnitID = Constants.BannerAd320x250SlotID } else if bannerType == .Banner1200x627 { banner = TADBannerView.init(adSize: TADAdSize1200x627RatioBannerWithWidth(300), origin: CGPoint(x: 10, y: self.view.frame.size.height - 450)) banner?.adUnitID = Constants.BannerAd1200x627SlotID } else if bannerType == .SmartBanner { banner = TADBannerView.init(adSize: kTADAdSizeSmartBannerPortrait, origin: CGPoint(x: 0, y: self.view.frame.size.height - 100)) banner?.adUnitID = Constants.BannerAd320x50SlotID } banner?.delegate = self banner?.adUnitID = slotId banner?.rootViewController = self banner?.turnOnMicrophone = false banner?.load(request) } }
TADBannerViewDelegate
extension BannerViewController: TADBannerViewDelegate { // Ad Request Lifecycle Notifications // 廣告取得成功 func adViewDidReceiveAd(_ view: TADBannerView!) { } // 廣告取得失敗 func adView(_ view: TADBannerView!, didFailToReceiveAdWithError error: TADRequestError!) { } // 廣告即將被呈現之前被呼叫 func adViewWillPresentScreen(_ adView: TADBannerView!) { } // 廣告即將被關閉之前被呼叫 func adViewWillDismissScreen(_ adView: TADBannerView!) { } // 廣告被關閉之後被呼叫 func adViewDidDismissScreen(_ adView: TADBannerView!) { } // 應用程式即將進入背景執行或中止運作前被呼叫 func adViewWillLeaveApplication(_ adView: TADBannerView!) { } }