SDK8 AdMob Mediation Banner
於 2023年4月7日 (五) 09:56 由 CharlesWang(留言 | 貢獻) 所做的修訂
簡介
首先先完成 mediation 基本 setup AdMob Mediation Setup, TADCustomEvent 將會透過此客製化 class 取得 TADBanner。
TAMedia Banner 廣告支援格式
廣告格式 (寬度x高度) | AdMob 對應常數值 |
---|---|
BANNER 320x50 | kGADAdSizeBanner |
BANNER 1200x627 | GADAdSizeFromCGSize(相對應比例CGSize) |
BANNER 320x50 | kGADAdSizeMediumRectangle |
實作方式
TADCustomEventBanner.swift
import Foundation
import GoogleMobileAds
import TAMediaAdsFramework
class TADCustomEventBanner: NSObject, GADMediationBannerAd {
var view: UIView {
return bannerView ?? UIView()
}
var bannerView: TADBannerView?
/// The ad event delegate to forward ad rendering events to the Google Mobile Ads SDK.
var delegate: GADMediationBannerAdEventDelegate?
/// Completion handler called after ad load
var completionHandler: GADMediationBannerLoadCompletionHandler?
func loadBanner(for adConfiguration: GADMediationBannerAdConfiguration,
completionHandler: @escaping GADMediationBannerLoadCompletionHandler) {
let tadRequest = TADRequest()
tadRequest.isTesting = adConfiguration.isTestRequest
tadRequest.setLocationWithLatitude(adConfiguration.userLatitude, longitude: adConfiguration.userLongitude, accuracy: adConfiguration.userLocationAccuracyInMeters)
let adSize = adConfiguration.adSize
let adRatio: CGFloat = floor((adSize.size.width/adSize.size.height)*100)/100
let ta1200x627AdRatio: CGFloat = floor((1200.0/627)*100)/100
/// 依據不同的 size 設定
if GADAdSizeEqualToSize(adSize, GADAdSizeBanner) {
self.bannerView = TADBannerView(adSize: kTADAdSizeBanner)
} else if GADAdSizeEqualToSize(adSize, GADAdSizeMediumRectangle) {
self.bannerView = TADBannerView(adSize: kTADAdSizeMediumRectangle)
} else if adRatio == ta1200x627AdRatio {
self.bannerView = TADBannerView(adSize: TADAdSize1200x627RatioBannerWithWidth(adSize.size.width))
} else {
let error = NSError(domain: "com.TAmedia.TADCustomBanner", code: GADErrorCode.mediationInvalidAdSize.rawValue, userInfo: nil)
delegate = completionHandler(nil, error)
}
self.completionHandler = completionHandler
self.bannerView?.delegate = self
self.bannerView?.adUnitID = adConfiguration.credentials.settings["parameter"] as? String
self.bannerView?.turnOnMicrophone = false
self.bannerView?.rootViewController = adConfiguration.topViewController
self.bannerView?.load(tadRequest)
}
}
// MARK: TADBannerViewDelegate
extension TADCustomEventBanner: TADBannerViewDelegate {
func adViewDidReceiveAd(_ view: TADBannerView!) {
print("CustomBanner - adViewDidReceiveAd!!")
if let handler = completionHandler {
delegate = handler(self, nil)
}
}
func adView(_ view: TADBannerView!, didFailToReceiveAdWithError error: TADRequestError!) {
print("CustomBanner - didFailToReceiveAdWithError!! error: \(String(describing: error))")
if let handler = completionHandler {
delegate = handler(nil, error)
}
}
func adViewWillPresentScreen(_ adView: TADBannerView!) {
print("CustomBanner - adViewWillPresentScreen!!")
}
func adViewWillDismissScreen(_ adView: TADBannerView!) {
print("CustomBanner - adViewWillDismissScreen!!")
}
func adViewDidDismissScreen(_ adView: TADBannerView!) {
print("CustomBanner - adViewDidDismissScreen!!")
}
func adViewWillLeaveApplication(_ adView: TADBannerView!) {
print("CustomBanner - adViewWillLeaveApplication!!")
delegate?.reportClick()
}
}
Banner 畫面展示
320x50 | 1200x627 |