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

出自TAMedia
跳至導覽 跳至搜尋
imported>Wikiuser
imported>Wikiuser
行 9: 行 9:
*<span style="font-size:16px;">MediationViewController.swift</span>
*<span style="font-size:16px;">MediationViewController.swift</span>
  '''// 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
                                      }
          )
        }   
}
   
   
'''// 使用 GADInterstitial 物件來介接 TADInterstitial'''
 
var interstitialView: GADInterstitialAd?


  '''// MediationViewController 的 view 消失前, 將 interstitialView 及其 delegate 設為 nil'''
  '''// MediationViewController 的 view 消失前, 將 interstitialView 及其 delegate 設為 nil'''
    deinit {
  override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
         if self.interstitialView != nil {
         if self.interstitialView != nil {
             self.interstitialView?.fullScreenContentDelegate = nil
             self.interstitialView?.fullScreenContentDelegate = nil
行 20: 行 42:
         }
         }
     }
     }
'''//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.")
    }
  }




行 54: 行 94:
         '''// 必須載入廣告'''
         '''// 必須載入廣告'''
         self.interstitialView?.load(tadRequest)
         self.interstitialView?.load(tadRequest)
     }
     }
    ''' '''
  ''' '''
     func present(fromRootViewController rootViewController: UIViewController) {
     func present(fromRootViewController rootViewController: UIViewController) {
         interstitialView?.present(fromRootViewController: rootViewController)
         interstitialView?.present(fromRootViewController: rootViewController)
行 82: 行 121:
         print("CustomInterstitial - interstitialWillDismissScreen!!")
         print("CustomInterstitial - interstitialWillDismissScreen!!")
         self.delegate?.customEventInterstitialWillDismiss(self)
         self.delegate?.customEventInterstitialWillDismiss(self)
        '''// interstitial 廣告按右上關閉時, 要將 interstitialView 及其 delegate 設為 nil'''
        if self.interstitialView != nil {
            self.interstitialView?.delegate = nil
            self.interstitialView = nil
        }
     }
     }
     ''' '''
     ''' '''

於 2021年8月27日 (五) 08:34 的修訂

簡介

首先將 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/mobile-ads-sdk/docs/admob/mediation#ios)
最後,在專案中加入與 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
                                      }
          )
       }     
}


// MediationViewController 的 view 消失前, 將 interstitialView 及其 delegate 設為 nil
  override func viewWillDisappear(_ animated: Bool) {
       super.viewWillDisappear(animated)
       if self.interstitialView != nil {
           self.interstitialView?.fullScreenContentDelegate = nil
           self.interstitialView = nil
       }
   }
//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)
   }
   
   func interstitialWillPresentScreen(_ ad: TADInterstitial!) {
       print("CustomInterstitial - interstitialWillPresentScreen!!")
       self.delegate?.customEventInterstitialWillPresent(self)
   }
   
   func interstitialDidDismissScreen(_ ad: TADInterstitial!) {
       print("CustomInterstitial - interstitialWillDismissScreen!!")
       self.delegate?.customEventInterstitialWillDismiss(self)
       // interstitial 廣告按右上關閉時, 要將 interstitialView 及其 delegate 設為 nil
       if self.interstitialView != nil {
           self.interstitialView?.delegate = nil
           self.interstitialView = nil
       }
   }
    
   func interstitialWillLeaveApplication(_ ad: TADInterstitial!) {
       print("CustomInterstitial - interstitialWillLeaveApplication!!")
       self.delegate?.customEventInterstitialWillLeaveApplication(self)
   }
}

Interstitial畫面展示

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