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

出自TAMedia
跳至導覽 跳至搜尋
imported>Wikiuser
(新頁面: == <b>簡介</b> == 首先將 AdMob 和 TAMedia SDK 整合至專案中,AdMob SDK可以在這裡下載(https://developers.google.com/mobile-ads-sdk/download?#downloadios)<br>''' 接...)
 
imported>Wikiuser
行 89: 行 89:
     }
     }
  }
  }
== Interstitial畫面展示 ==
{|  border="1"
|-
| align="center" | 插頁式廣告呈現
|-
| align="center" | [[檔案:2021-08-06-AdMob_Interstitial.jpeg]]
|}

於 2021年8月6日 (五) 01:38 的修訂

簡介

首先將 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

// 使用 GADInterstitial 物件來介接 TADInterstitial
var interstitialView: GADInterstitialAd?
// MediationViewController 的 view 消失前, 將 interstitialView 及其 delegate 設為 nil
   deinit {
       if self.interstitialView != nil {
           self.interstitialView?.fullScreenContentDelegate = nil
           self.interstitialView = nil
       }
   }


  • 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)
   }
    
   func interstitialWillLeaveApplication(_ ad: TADInterstitial!) {
       print("CustomInterstitial - interstitialWillLeaveApplication!!")
       self.delegate?.customEventInterstitialWillLeaveApplication(self)
   }
}

Interstitial畫面展示

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