「SDK8 AdMob Mediation Banner」修訂間的差異

出自TAMedia
跳至導覽 跳至搜尋
 
(未顯示同一使用者於中間所作的 1 次修訂)
行 1: 行 1:
== <b>簡介</b> ==
首先將 AdMob 和 TAMedia SDK 整合至專案中,AdMob SDK可以在這裡下載(https://developers.google.com/mobile-ads-sdk/download?#downloadios)<br>'''
接著在 AdMob Mediation 新增 Custom Event (https://mediation.admob.com) <br>'''
新增 Custom Event 的詳細說明可以參考 (https://developers.google.com/admob/ios/custom-events/banner) <br>'''
最後,在專案中加入與 Custom Event 對應的 Class,其中 Custom Event 的 Class Name 需要和專案新增的 Class Name 保持一致
例如新增的 Class Name 為 TADCustomBanner,Custom Event 的 Class Name 則必須是 專案名稱.TADCustomBanner。(以 Swift 編寫的專案需加上專案名稱;若為以 OC編寫,則放入 Class Name)
<br><br>
== <b>TAMedia Banner 廣告支援格式</b> ==
{| class="wikitable" style="width:50%"
|-
! 廣告格式 (寬度x高度) !! AdMob 對應常數值
|-
| align="center"|BANNER 320x50 || align="center"|kGADAdSizeBanner
|-
| align="center"|BANNER 1200x627 || align="center"|GADAdSizeFromCGSize(相對應比例CGSize)
|-
| align="center"|BANNER 320x50 || align="center"|kGADAdSizeMediumRectangle
|-
|}
<br><br>
== <b>實作方式</b> ==
*<span style="font-size:16px;">MediationViewController.swift</span>
'''// 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
  }
}
*<span style="font-size:16px;">Custom Banner View</span>
'''// 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)
    }
}
<br><br>
== <b> Banner 畫面展示</b> ==
{| width="70%" border="1"
|-
| align="center" | 320x50
| align="center" | 1200x627
|-
| align="center" | [[檔案:2021-08-06-AdMob_Banner320.PNG|x300px]]
| align="center" | [[檔案:2021-08-06-AdMob_Banner1200.PNG|x300px]]
|}
[[SDK8 Google AdMob Mediation | 回到 Mediation 首頁]]
<!--
== <b>簡介</b> ==
== <b>簡介</b> ==
首先先完成 mediation 基本 setup [[AdMob Mediation Setup]], TADCustomEvent 將會透過此客製化 class 取得 TADBanner。
首先先完成 mediation 基本 setup [[AdMob Mediation Setup]], TADCustomEvent 將會透過此客製化 class 取得 TADBanner。
行 254: 行 116:
|}
|}


-->
[[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()
    }
}

320x50 1200x627
2021-08-06-AdMob Banner320.PNG 2021-08-06-AdMob Banner1200.PNG

回 AdMob Mediation