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

出自TAMedia
跳至導覽 跳至搜尋
imported>Wikiuser
 
(未顯示由 2 位使用者於中間所作的 5 次修訂)
行 1: 行 1:
== <b>簡介</b> ==
== <b>簡介</b> ==
首先將 AdMob 和 TAMedia SDK 整合至專案中,AdMob SDK可以在這裡下載(https://developers.google.com/mobile-ads-sdk/download?#downloadios)<br>'''
首先先完成 mediation 基本 setup [[AdMob Mediation Setup]], TADCustomEvent 將會透過此客製化 class 取得 TADInterstitial。
接著在 AdMob Mediation 新增 Custom Event (https://mediation.admob.com) <br>'''
 
新增 Custom Event 的詳細說明可以參考 (https://developers.google.com/mobile-ads-sdk/docs/admob/mediation#ios) <br>'''
最後,在專案中加入與 Custom Event 對應的 Class,其中 Custom Event 的 Class Name 需要和專案新增的 Class Name 保持一致
例如新增的 Class Name 為 TADCustom Interstitial,Custom Event 的 Class Name 則必須是 專案名稱.TADCustom Interstitial。(以 Swift 編寫的專案需加上專案名稱;若為以 OC編寫,則放入 Class Name)
<br><br>
== <b>實作方式</b> ==
== <b>實作方式</b> ==
*<span style="font-size:16px;">MediationViewController.swift</span>
TADCustomEventInterstitial.swift
'''// MediationViewController.swift'''
<source>
import UIKit
class TADCustomEventInterstitial: NSObject, GADMediationInterstitialAd {
'''// 匯入 Admob SDK 定義'''
    var delegate: GADMediationInterstitialAdEventDelegate?
import GoogleMobileAds
    var interstitialView: TADInterstitial?
class MediationViewController: UIViewController, GADFullScreenContentDelegate {
    var completionHandler: GADMediationInterstitialLoadCompletionHandler?
      '''// 使用 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'''
     override required init() {
  extension MediationViewController {
        super.init()
    /// 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.")
    }
  }


 
     func loadInterstitial(for adConfiguration: GADMediationInterstitialAdConfiguration,
*<span style="font-size:16px;">Custom Banner View</span>
        completionHandler: @escaping GADMediationInterstitialLoadCompletionHandler) {
'''// TADCustomInterstitial.swift'''
         // 將 adConfiguration 物件裡的資訊轉入 TADRequest 物件
'''// 匯入 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()
         let tadRequest = TADRequest()
         tadRequest.isTesting = request.isTesting
         tadRequest.isTesting = adConfiguration.isTestRequest
        tadRequest.location = request.userLocationDescription
         tadRequest.setLocationWithLatitude(adConfiguration.userLatitude, longitude: adConfiguration.userLongitude, accuracy: adConfiguration.userLocationAccuracyInMeters)
         tadRequest.setLocationWithLatitude(request.userLatitude, longitude: request.userLongitude, accuracy: request.userLocationAccuracyInMeters)
          
         ''' '''
         self.completionHandler = completionHandler
         ''' //必須將 interstitialView 物件初始化'''
         self.interstitialView = TADInterstitial()
         self.interstitialView = TADInterstitial()
        '''// 必須設定delegate'''
         self.interstitialView?.delegate = self
         self.interstitialView?.delegate = self
        '''// 必須設定 adUnitID,serverParameter 為在 Admob 裡設定的 AD Unit ID'''
         self.interstitialView?.adUnitID = adConfiguration.credentials.settings["parameter"] as? String
         self.interstitialView?.adUnitID = serverParameter
         self.interstitialView?.turnOnMicrophone = false
         '''// 必須載入廣告'''
         self.interstitialView?.load(tadRequest)
         self.interstitialView?.load(tadRequest)
     }
     }
  ''' '''
   
     func present(fromRootViewController rootViewController: UIViewController) {
     func present(from viewController: UIViewController) {
         interstitialView?.present(fromRootViewController: rootViewController)
         interstitialView?.present(fromRootViewController: viewController)
     }
     }
}
}
 
// MARK: TADInterstitialDelegate


'''//MARK: TADInterstitialDelegate'''
extension TADCustomEventInterstitial: TADInterstitialDelegate {
extension TADCustomInterstitial {
     func interstitialDidReceiveAd(_ ad: TADInterstitial!) {
     func interstitialDidReceiveAd(_ ad: TADInterstitial!) {
         print("CustomInterstitial - interstitialDidReceiveAd!!")
         print("CustomInterstitial - interstitialDidReceiveAd!!")
         self.delegate?.customEventInterstitialDidReceiveAd(self)
         if let handler = completionHandler {
            delegate = handler(self, nil)
        }
     }
     }
  ''' '''
 
     func interstitial(_ ad: TADInterstitial!, didFailToReceiveAdWithError error: TADRequestError!) {
     func interstitial(_ ad: TADInterstitial!, didFailToReceiveAdWithError error: TADRequestError!) {
         print("CustomInterstitial - didFailToReceiveAdWithError!! error: \(String(describing: error))")
         print("CustomInterstitial - didFailToReceiveAdWithError!! error: \(String(describing: error))")
         self.delegate?.customEventInterstitial(self, didFailAd: 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!) {
     func interstitialWillPresentScreen(_ ad: TADInterstitial!) {
         print("CustomInterstitial - interstitialWillPresentScreen!!")
         print("CustomInterstitial - interstitialWillPresentScreen!!")
         self.delegate?.customEventInterstitialWillPresent(self)
         delegate?.reportImpression()
     }
     }
  ''' '''
 
     func interstitialDidDismissScreen(_ ad: TADInterstitial!) {
     func interstitialDidDismissScreen(_ ad: TADInterstitial!) {
         print("CustomInterstitial - interstitialWillDismissScreen!!")
         print("CustomInterstitial - interstitialWillDismissScreen!!")
         self.delegate?.customEventInterstitialWillDismiss(self)
         delegate?.didDismissFullScreenView()
     }
     }
    ''' '''
 
     func interstitialWillLeaveApplication(_ ad: TADInterstitial!) {
     func interstitialWillLeaveApplication(_ ad: TADInterstitial!) {
         print("CustomInterstitial - interstitialWillLeaveApplication!!")
         print("CustomInterstitial - interstitialWillLeaveApplication!!")
         self.delegate?.customEventInterstitialWillLeaveApplication(self)
         delegate?.reportClick()
     }
     }
}
}
</source>


== Interstitial畫面展示 ==
== Interstitial畫面展示 ==
行 125: 行 78:
| align="center" | [[檔案:2021-08-06-AdMob_Interstitial.jpeg|x300px]]
| align="center" | [[檔案:2021-08-06-AdMob_Interstitial.jpeg|x300px]]
|}
|}
[[SDK8 Google AdMob Mediation|回 AdMob Mediation]]

於 2023年4月7日 (五) 09:56 的最新修訂

簡介

首先先完成 mediation 基本 setup AdMob Mediation Setup, TADCustomEvent 將會透過此客製化 class 取得 TADInterstitial。

實作方式

TADCustomEventInterstitial.swift

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

Interstitial畫面展示

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

回 AdMob Mediation