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

出自TAMedia
跳至導覽 跳至搜尋
imported>Wikiuser
行 129: 行 129:
| align="center" | [[檔案:2021-08-06-AdMob_Interstitial.jpeg|x300px]]
| align="center" | [[檔案:2021-08-06-AdMob_Interstitial.jpeg|x300px]]
|}
|}
<!--
== <b>簡介</b> ==
首先先完成 mediation 基本 setup [[AdMob Mediation Setup]], TADCustomEvent 將會透過此客製化 class 取得 TADInterstitial。
== <b>實作方式</b> ==
TADCustomEventInterstitial.swift
<source>
class TADCustomEventInterstitial: NSObject, GADMediationInterstitialAd {
    var delegate: GADMediationInterstitialAdEventDelegate?
    var interstitialView: TADInterstitial?
    var completionHandler: GADMediationInterstitialLoadCompletionHandler?
    override required init() {
        super.init()
    }
    func loadInterstitial(for adConfiguration: GADMediationInterstitialAdConfiguration,
        completionHandler: @escaping GADMediationInterstitialLoadCompletionHandler) {
        // 將 adConfiguration 物件裡的資訊轉入 TADRequest 物件
        let tadRequest = TADRequest()
        tadRequest.isTesting = adConfiguration.isTestRequest
        tadRequest.setLocationWithLatitude(adConfiguration.userLatitude, longitude: adConfiguration.userLongitude, accuracy: adConfiguration.userLocationAccuracyInMeters)
       
        self.completionHandler = completionHandler
        self.interstitialView = TADInterstitial()
        self.interstitialView?.delegate = self
        self.interstitialView?.adUnitID = adConfiguration.credentials.settings["parameter"] as? String
        self.interstitialView?.turnOnMicrophone = false
        self.interstitialView?.load(tadRequest)
    }
   
    func present(from viewController: UIViewController) {
        interstitialView?.present(fromRootViewController: viewController)
    }
}
// MARK: TADInterstitialDelegate
extension TADCustomEventInterstitial: TADInterstitialDelegate {
    func interstitialDidReceiveAd(_ ad: TADInterstitial!) {
        print("CustomInterstitial - interstitialDidReceiveAd!!")
        if let handler = completionHandler {
            delegate = handler(self, nil)
        }
    }
    func interstitial(_ ad: TADInterstitial!, didFailToReceiveAdWithError error: TADRequestError!) {
        print("CustomInterstitial - didFailToReceiveAdWithError!! error: \(String(describing: error))")
        if let handler = completionHandler {
            delegate = handler(nil, error)
        }
        // 需手動設為 nil,auto release 在 admob SDK 會造成在未在 main thread 執行
        self.interstitialView?.delegate = nil
        self.interstitialView = nil
    }
    func interstitialWillPresentScreen(_ ad: TADInterstitial!) {
        print("CustomInterstitial - interstitialWillPresentScreen!!")
        delegate?.reportImpression()
    }
    func interstitialDidDismissScreen(_ ad: TADInterstitial!) {
        print("CustomInterstitial - interstitialWillDismissScreen!!")
        delegate?.didDismissFullScreenView()
    }
    func interstitialWillLeaveApplication(_ ad: TADInterstitial!) {
        print("CustomInterstitial - interstitialWillLeaveApplication!!")
        delegate?.reportClick()
    }
}
</source>
== Interstitial畫面展示 ==
{|  border="1"
|-
| align="center" | 插頁式廣告呈現
|-
| align="center" | [[檔案:2021-08-06-AdMob_Interstitial.jpeg|x300px]]
|}
-->

於 2023年3月1日 (三) 10:04 的修訂

簡介

首先將 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/interstitial)
最後,在專案中加入與 Custom Event 對應的 Class,其中 Custom Event 的 Class Name 需要和專案新增的 Class Name 保持一致 例如新增的 Class Name 為 TADCustom Interstitial,Custom Event 的 Class Name 則必須是 專案名稱.TADCustom Interstitial。(以 Swift 編寫的專案需加上專案名稱;若為以 OC編寫,則放入 Class Name)

實作方式

  • MediationViewController.swift
// MediationViewController.swift
import UIKit
// 匯入 Admob SDK 定義
import GoogleMobileAds
class MediationViewController: UIViewController, GADFullScreenContentDelegate {
     // 使用 GADInterstitial 物件來介接 TADInterstitial
      var interstitialView: GADInterstitialAd?
      func viewDidLoad() {
         super.viewDidLoad()
     // 使用 GADRequest 載入AdMob設定之廣告
         let request = GADRequest()
         GADInterstitialAd.load(withAdUnitID:"ca-app-pub-AdUnitID",
                                                        request: request,
                                     completionHandler: { [self] ad, error in
                                        if let error = error {
                                           print("Failed to load interstitial ad with error: \(error.localizedDescription)")
                                           return
                                        }
                                       interstitial = ad
                                      }
          )
       }     
}
//MARK: GADFullScreenContentDelegate
 extension MediationViewController {
   /// Tells the delegate that the ad failed to present full screen content.
   func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
     print("Ad did fail to present full screen content.")
   }
     
   /// Tells the delegate that the ad presented full screen content.
   func adDidPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
     print("Ad did present full screen content.")
   }
     
   /// Tells the delegate that the ad dismissed full screen content.
   func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
     print("Ad did dismiss full screen content.")
   }
 }


  • Custom Banner View
// TADCustomInterstitial.swift
// 匯入 TAMedia SDK 定義
import TAMediaAdsFramework
// 匯入 Admob SDK 定義
import GoogleMobileAds

class TADCustomInterstitial: NSObject, GADCustomEventInterstitial, TADInterstitialDelegate {
  // 宣告 TADInterstitial 物件
  var interstitialView: TADInterstitial?
  // 宣告 GADCustomEventInterstitialDelegate 物件
  var delegate: GADCustomEventInterstitialDelegate?
   
  required override init() {
      super.init()
  }
   
   func requestAd(withParameter serverParameter: String?, label serverLabel: String?, request: GADCustomEventRequest) {
       // 將 GADCustomEventRequest 物件裡的資訊轉入 TADRequest 物件
       let tadRequest = TADRequest()
       tadRequest.isTesting = request.isTesting
       tadRequest.location = request.userLocationDescription
       tadRequest.setLocationWithLatitude(request.userLatitude, longitude: request.userLongitude, accuracy: request.userLocationAccuracyInMeters)
        
        //必須將 interstitialView 物件初始化
       self.interstitialView = TADInterstitial()
       // 必須設定delegate
       self.interstitialView?.delegate = self
       // 必須設定 adUnitID,serverParameter 為在 Admob 裡設定的 AD Unit ID
       self.interstitialView?.adUnitID = serverParameter
       // 必須載入廣告
       self.interstitialView?.load(tadRequest)
   }
   
   func present(fromRootViewController rootViewController: UIViewController) {
       interstitialView?.present(fromRootViewController: rootViewController)
   }
}
//MARK: TADInterstitialDelegate
extension TADCustomInterstitial {
   func interstitialDidReceiveAd(_ ad: TADInterstitial!) {
       print("CustomInterstitial - interstitialDidReceiveAd!!")
       self.delegate?.customEventInterstitialDidReceiveAd(self)
   }
   
   func interstitial(_ ad: TADInterstitial!, didFailToReceiveAdWithError error: TADRequestError!) {
       print("CustomInterstitial - didFailToReceiveAdWithError!! error: \(String(describing: error))")
       self.delegate?.customEventInterstitial(self, didFailAd: error)
       // 將 TAmedia 廣告設為 nil
       self.interstitialView?.delegate = nil
       self.interstitialView = nil      
   }
   
   func interstitialWillPresentScreen(_ ad: TADInterstitial!) {
       print("CustomInterstitial - interstitialWillPresentScreen!!")
       self.delegate?.customEventInterstitialWillPresent(self)
   }
   
   func interstitialDidDismissScreen(_ ad: TADInterstitial!) {
       print("CustomInterstitial - interstitialWillDismissScreen!!")
       self.delegate?.customEventInterstitialWillDismiss(self)
   }
    
   func interstitialWillLeaveApplication(_ ad: TADInterstitial!) {
       print("CustomInterstitial - interstitialWillLeaveApplication!!")
       self.delegate?.customEventInterstitialWillLeaveApplication(self)
   }
}

Interstitial畫面展示

插頁式廣告呈現
2021-08-06-AdMob Interstitial.jpeg