AdMob Mediation Setup
於 2023年3月13日 (一) 07:50 由 CharlesWang(留言 | 貢獻) 所做的修訂
簡介
首先將 AdMob 和 TAMedia SDK 整合至專案中,AdMob SDK可以在這裡下載(https://developers.google.com/mobile-ads-sdk/download?#downloadios)
接著在 AdMob Mediation 新增 Custom Event,新增 Custom Event 的詳細說明可以參考 (https://developers.google.com/admob/ios/custom-events/setup)
AdMob 將透過此客製化 class 向 TAmedia 請求廣告取得 Banner, Interstitial 及 Native 廣告。
在專案中加入與 Custom Event 對應的 Class,其中 Custom Event 的 Class Name 需要和專案新增的 Class Name 保持一致
例如新增的 Class Name 為 TADCustomEvent,Custom Event 的 Class Name 則必須是 專案名稱.TADCustomEvent。(以 Swift 編寫的專案需加上專案名稱;若為以 OC編寫,則放入 Class Name,https://developers.google.com/admob/ios/custom-events/setup#create)
實作方式
import Foundation
import GoogleMobileAds
import TAMediaAdsFramework
class TADCustomEvent: NSObject, GADMediationAdapter {
private var customEventNative: TADCustomEventNative?
private var customEventBanner: TADCustomEventBanner?
private var customEventInterstitial: TADCustomEventInterstitial?
override required init() {
super.init()
}
deinit {
print("TADCustomEvent deinit")
}
static func adapterVersion() -> GADVersionNumber {
return GADVersionNumber(majorVersion: 2, minorVersion: 0, patchVersion: 0)
}
static func adSDKVersion() -> GADVersionNumber {
let version = TADMobileAds.getSDKVersion().components(separatedBy: ".")
let major = Int(version[0]) ?? 0
let minor = Int(version[1]) ?? 0
let patch = Int(version[2]) ?? 0
return GADVersionNumber(majorVersion: major, minorVersion: minor, patchVersion: patch)
}
static func networkExtrasClass() -> GADAdNetworkExtras.Type? {
return nil
}
static func setUpWith(_ configuration: GADMediationServerConfiguration, completionHandler: @escaping GADMediationAdapterSetUpCompletionBlock) {
TADMobileAds.config()
completionHandler(nil)
}
func loadBanner(for adConfiguration: GADMediationBannerAdConfiguration, completionHandler: @escaping GADMediationBannerLoadCompletionHandler) {
customEventBanner = TADCustomEventBanner()
customEventBanner?.loadBanner(for: adConfiguration, completionHandler: completionHandler)
}
func loadInterstitial(for adConfiguration: GADMediationInterstitialAdConfiguration, completionHandler: @escaping GADMediationInterstitialLoadCompletionHandler) {
customEventInterstitial = TADCustomEventInterstitial()
customEventInterstitial?.loadInterstitial(for: adConfiguration, completionHandler: completionHandler)
}
func loadNativeAd(for adConfiguration: GADMediationNativeAdConfiguration, completionHandler: @escaping GADMediationNativeLoadCompletionHandler) {
customEventNative = TADCustomEventNative()
customEventNative?.loadNativeAd(for: adConfiguration, completionHandler: completionHandler)
}
}