「SDK8 AdMob Mediation Banner」修訂間的差異
跳至導覽
跳至搜尋
imported>Wikiuser (→實作方式) |
CharlesWang(留言 | 貢獻) |
||
(未顯示由 2 位使用者於中間所作的 6 次修訂) | |||
行 1: | 行 1: | ||
== <b>簡介</b> == | == <b>簡介</b> == | ||
首先先完成 mediation 基本 setup [[AdMob Mediation Setup]], TADCustomEvent 將會透過此客製化 class 取得 TADBanner。 | |||
== <b>TAMedia Banner 廣告支援格式</b> == | == <b>TAMedia Banner 廣告支援格式</b> == | ||
{| class="wikitable" style="width:50%" | {| class="wikitable" style="width:50%" | ||
行 20: | 行 15: | ||
|} | |} | ||
== <b>實作方式</b> == | == <b>實作方式</b> == | ||
TADCustomEventBanner.swift | |||
<source> | |||
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() | let tadRequest = TADRequest() | ||
tadRequest.isTesting = adConfiguration.isTestRequest | |||
tadRequest.isTesting = | tadRequest.setLocationWithLatitude(adConfiguration.userLatitude, longitude: adConfiguration.userLongitude, accuracy: adConfiguration.userLocationAccuracyInMeters) | ||
let adSize = adConfiguration.adSize | |||
tadRequest.setLocationWithLatitude( | 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?.delegate = self | ||
self.bannerView?.adUnitID = adConfiguration.credentials.settings["parameter"] as? String | |||
self.bannerView?.adUnitID = | |||
self.bannerView?.turnOnMicrophone = false | self.bannerView?.turnOnMicrophone = false | ||
self.bannerView?.rootViewController = adConfiguration.topViewController | |||
self.bannerView?.load(tadRequest) | self.bannerView?.load(tadRequest) | ||
} | } | ||
} | |||
// MARK: TADBannerViewDelegate | |||
extension TADCustomEventBanner: TADBannerViewDelegate { | |||
func adViewDidReceiveAd(_ view: TADBannerView!) { | func adViewDidReceiveAd(_ view: TADBannerView!) { | ||
print("CustomBanner - adViewDidReceiveAd!!") | |||
if let handler = completionHandler { | |||
delegate = handler(self, nil) | |||
} | |||
} | } | ||
func adView(_ view: TADBannerView!, didFailToReceiveAdWithError error: TADRequestError!) { | 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!) { | func adViewWillPresentScreen(_ adView: TADBannerView!) { | ||
print("CustomBanner - adViewWillPresentScreen!!") | print("CustomBanner - adViewWillPresentScreen!!") | ||
} | } | ||
func adViewWillDismissScreen(_ adView: TADBannerView!) { | func adViewWillDismissScreen(_ adView: TADBannerView!) { | ||
print("CustomBanner - adViewWillDismissScreen!!") | print("CustomBanner - adViewWillDismissScreen!!") | ||
} | } | ||
func adViewDidDismissScreen(_ adView: TADBannerView!) { | func adViewDidDismissScreen(_ adView: TADBannerView!) { | ||
print("CustomBanner - adViewDidDismissScreen!!") | print("CustomBanner - adViewDidDismissScreen!!") | ||
} | } | ||
func adViewWillLeaveApplication(_ adView: TADBannerView!) { | func adViewWillLeaveApplication(_ adView: TADBannerView!) { | ||
print("CustomBanner - adViewWillLeaveApplication!!") | print("CustomBanner - adViewWillLeaveApplication!!") | ||
delegate?. | delegate?.reportClick() | ||
} | |||
} | |||
< | </source> | ||
== <b> Banner 畫面展示</b> == | == <b> Banner 畫面展示</b> == | ||
行 149: | 行 115: | ||
| align="center" | [[檔案:2021-08-06-AdMob_Banner1200.PNG|x300px]] | | align="center" | [[檔案:2021-08-06-AdMob_Banner1200.PNG|x300px]] | ||
|} | |} | ||
[[SDK8 Google AdMob Mediation|回 AdMob Mediation]] |
於 2023年4月7日 (五) 09:56 的最新修訂
簡介
首先先完成 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 |