SDK8 AdMob Mediation Banner
於 2023年3月1日 (三) 09:46 由 CharlesWang(留言 | 貢獻) 所做的修訂
簡介
首先將 AdMob 和 TAMedia SDK 整合至專案中,AdMob SDK可以在這裡下載(https://developers.google.com/mobile-ads-sdk/download?#downloadios)
接著在 AdMob Mediation 新增 Custom Event (https://mediation.admob.com)
新增 Custom Event 的詳細說明可以參考 (https://developers.google.com/admob/ios/custom-events/banner)
最後,在專案中加入與 Custom Event 對應的 Class,其中 Custom Event 的 Class Name 需要和專案新增的 Class Name 保持一致
例如新增的 Class Name 為 TADCustomBanner,Custom Event 的 Class Name 則必須是 專案名稱.TADCustomBanner。(以 Swift 編寫的專案需加上專案名稱;若為以 OC編寫,則放入 Class Name)
TAMedia Banner 廣告支援格式
廣告格式 (寬度x高度) | AdMob 對應常數值 |
---|---|
BANNER 320x50 | kGADAdSizeBanner |
BANNER 1200x627 | GADAdSizeFromCGSize(相對應比例CGSize) |
BANNER 320x50 | kGADAdSizeMediumRectangle |
實作方式
- MediationViewController.swift
// MediationViewController.swift // 匯入 Admob SDK 定義 import GoogleMobileAds class MediationViewController: UIViewController, GADBannerViewDelegate { // 使用 GADBannerView 物件來介接 TADBannerView var bannerView: GADBannerView! func viewDidLoad() { super.viewDidLoad() // 初始化 bannerView,根據需求設定大小 bannerView = GADBannerView(adSize: kGADAdSizeBanner) // 自行定義method 將 bannerView addSubview 至畫面 addBannerViewToView(bannerView) self.bannerView.adUnitID = "ca-app-pub-adUnitID" self.bannerView.rootViewController = self self.bannerView.delegate = self } }
- Custom Banner View
// TADCustomBanner.swift // 匯入 TAMedia SDK 定義 import TAMediaAdsFramework // 匯入 Admob SDK 定義 import GoogleMobileAds class TADCustomBanner: NSObject, GADCustomEventBanner, TADBannerViewDelegate { // 宣告 TADBannerView 物件 var bannerView: TADBannerView? // 宣告 GADCustomEventBannerDelegate 物件 var delegate: GADCustomEventBannerDelegate? required override init() { super.init() } func requestAd(_ adSize: GADAdSize, parameter serverParameter: String?, label serverLabel: String?, request: GADCustomEventRequest) { // 將 GADCustomEventRequest 物件裡的資訊轉入 TADRequest 物件 let tadRequest = TADRequest() tadRequest.showLog = true tadRequest.isTesting = request.isTesting tadRequest.location = request.userLocationDescription tadRequest.setLocationWithLatitude(request.userLatitude, longitude: request.userLongitude, accuracy: request.userLocationAccuracyInMeters) // 必須將 bannerView 物件初始化 // 若是你使用banner 1200 627, 請記得要使用 TADAdSize1200x627RatioBannerWithWidth 或 TADAdSize1200x627RatioBannerWithHeight // 因為此為 TAMedia 特別定義的格式 // e.g. // self.bannerView = TADBannerView(adSize: TADAdSize1200x627RatioBannerWithWidth(adSize.size.width)) // self.bannerView = TADBannerView(adSize: TADAdSizeFromCGSize(adSize.size)) // 必須設定delegate self.bannerView?.delegate = self // 必須設定 adUnitID,serverParameter 為在 Admob 裡設定的 AD Unit ID self.bannerView?.adUnitID = serverParameter // 必需設定rootViewController self.bannerView?.rootViewController = UIApplication.shared.keyWindow?.rootViewController?.presentedViewController // 設定廣告若為影片時的聲音開關 self.bannerView?.turnOnMicrophone = false // 必須載入廣告 self.bannerView?.load(tadRequest) } }
//MARK: TADBannerViewDelegate extension TADCustomBanner { func adViewDidReceiveAd(_ view: TADBannerView!) { print("CustomBanner - adViewDidReceiveAd!!") delegate?.customEventBanner(self, didReceiveAd: view) } func adView(_ view: TADBannerView!, didFailToReceiveAdWithError error: TADRequestError!) { print("CustomBanner - didFailToReceiveAdWithError!! error: \(String(describing: error))") } func adViewWillPresentScreen(_ adView: TADBannerView!) { print("CustomBanner - adViewWillPresentScreen!!") delegate?.customEventBannerWillPresentModal(self) } func adViewWillDismissScreen(_ adView: TADBannerView!) { print("CustomBanner - adViewWillDismissScreen!!") delegate?.customEventBannerWillDismissModal(self) } func adViewDidDismissScreen(_ adView: TADBannerView!) { print("CustomBanner - adViewDidDismissScreen!!") delegate?.customEventBannerDidDismissModal(self) } func adViewWillLeaveApplication(_ adView: TADBannerView!) { print("CustomBanner - adViewWillLeaveApplication!!") delegate?.customEventBannerWillLeaveApplication(self) } }
Banner 畫面展示
320x50 | 1200x627 |