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

出自TAMedia
跳至導覽 跳至搜尋
 
行 238: 行 238:
}
}
</source>
</source>
[[SDK8 Google AdMob Mediation|回 AdMob Mediation]]

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

簡介

將說明如何將 TAmedia SDK 原生廣告與 Google AdMob 原生廣告(Native advanced)做 mediation 整合,關於 AdMob mediation 請參考:https://developers.google.com/admob/ios/mediate

串接準備

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


實作方式

TADCustomEventNative.swift

import Foundation
import GoogleMobileAds
import TAMediaAdsFramework


class TADCustomEventNative: NSObject {
    var tadNativeAd: TADNativeAd?
    var tadNativeAdView: TADNativeAdView?
    var mappedImages = [GADNativeAdImage]()
    var mapIcon: GADNativeAdImage?
    
    var delegate: GADMediationNativeAdEventDelegate?
    /// Completion handler called after ad load
    var completionHandler: GADMediationNativeLoadCompletionHandler?

    deinit {
        print("TADCustomEventNative - TADMediatedUnifiedNativeAd deinit")
    }
    
    func loadNativeAd(for adConfiguration: GADMediationNativeAdConfiguration,
        completionHandler: @escaping GADMediationNativeLoadCompletionHandler) {
        self.completionHandler = completionHandler
        let request = TADRequest()
        request.showLog = true
        let option = TADNativeAdOptions()
        /// 參見 http://wiki.tamedia.com.tw/iosDoc/Classes/TADNativeAdOptions.html
//        option.disableImageLoading = true //SDK 將不自動下載圖片
//        option.mediaPreferImage = true //只會呈現圖片廣告
//        option.startUnmuted = true     //影片廣告會以非靜音方式呈現
//        option.customControlsRequested = true //自行定義影音廣告的靜音/取消靜音、倒計時標籤和了解更多按鈕
//        option.allowAudioSessionControl = false //設為false則需自行設定影音廣告出現時 AVAudioSession 狀態
        tadNativeAd = TADNativeAd(adUnitId: adConfiguration.credentials.settings["parameter"] as? String)
        tadNativeAd?.delegate = self
        tadNativeAd?.load(request, withOption: option)
    }
    
    private func setup(nativeAd: TADNativeAd, nativeAdView: TADNativeAdView) {
        tadNativeAd = nativeAd
        tadNativeAdView = nativeAdView
        
        tadNativeAd?.delegate = self
        tadNativeAdView?.mediaView?.statusDelegate = self
        guard let mediaContent = tadNativeAd?.adContent.mediaContent else {
            return
        }
        if let tadImage = mediaContent.image1200x627?.image {
            let adImage = GADNativeAdImage(image: tadImage)
            tadNativeAdView?.mediaView?.setMainImage(image: tadImage)
            mappedImages.append(adImage)
        }
        if let iconImage = tadNativeAd?.adContent.iconSquare?.image {
            mapIcon = GADNativeAdImage(image: iconImage)
        }
    }
}

extension TADCustomEventNative: TADNativeAdDelegate {
    func nativeAdDidReceive(_ ad: TADNativeAd!) {
        let nativeAdView = TADNativeAdView()
        let mediaView = TADNativeMediaView(frame: .zero)
        mediaView.mediaContent = ad.adContent.mediaContent
        /// 參見 http://wiki.tamedia.com.tw/iosDoc/Classes/TADNativeMediaView.html
//        mediaView.setCallToActionVisible(false) //是否顯示 CTA 按鈕
//        mediaView.setCallToActionTextSize(12) //設定 CTA 文字大小
//        mediaView.setVolumeImageSize(24) //設定影音廣告靜音圖片大小
//        mediaView.setVideoCountdownTextSize(17) //設定影音倒數文字大小
        nativeAdView.addSubview(mediaView)
        nativeAdView.mediaView = mediaView
        
        setup(nativeAd: ad, nativeAdView: nativeAdView)
        
        if let handler = completionHandler {
            delegate = handler(self, nil)
        }
    }
    
    func nativeAd(_ ad: TADNativeAd!, didFailToReceiveAdWithError error: TADRequestError!) {
        if let handler = completionHandler {
            delegate = handler(nil, error)
        }
    }
    
    func nativeAdDidImpression(_ ad: TADNativeAd!) {
        print("TADCustomEventNative - nativeAdDidImpression")
        delegate?.reportImpression()
    }
    
    func nativeAdDidClick(_ ad: TADNativeAd!) {
        print("TADCustomEventNative - nativeAdDidClick")
        delegate?.reportClick()
    }

}

extension TADCustomEventNative: TADVideoStatusDelegate {
    func didStartVideo() {
        delegate?.didPlayVideo()
    }
    
    func didPlayVideo() {
        delegate?.didPlayVideo()
    }
    
    func didPauseVideo() {
        delegate?.didPauseVideo()
    }
    
    func didEndVideoPlayback() {
        delegate?.didEndVideo()
    }
    
    func didMuteVideo() {
        delegate?.didMuteVideo()
    }
    
    func didUnmuteVideo() {
        delegate?.didUnmuteVideo()
    }
}

extension TADCustomEventNative: GADMediationNativeAd {
    var headline: String? {
        return tadNativeAd?.adContent.longSubject
    }
    
    var images: [GADNativeAdImage]? {
        return mappedImages
    }
    
    var body: String? {
        return tadNativeAd?.adContent.body
    }
    
    var icon: GADNativeAdImage? {
        return mapIcon
    }
    
    var callToAction: String? {
        return tadNativeAd?.adContent.callToAction
    }
    
    var starRating: NSDecimalNumber? {
        return nil
    }
    
    var store: String? {
        return nil
    }
    
    var price: String? {
        return nil
    }
    
    var advertiser: String? {
        return nil
    }
    
    var extraAssets: [String: Any]? {
        return nil
    }
    
    var mediaView: UIView? {
        return tadNativeAdView?.mediaView
    }
    
    var hasVideoContent: Bool {
        guard let mediaContent = tadNativeAd?.adContent.mediaContent else {
            return false
        }
        return mediaContent.isVideoContent
    }
    
    func handlesUserClicks() -> Bool {
      return true
    }
    
    func handlesUserImpressions() -> Bool {
      return true
    }
    
    func didRender(in view: UIView, clickableAssetViews: [GADNativeAssetIdentifier : UIView], nonclickableAssetViews: [GADNativeAssetIdentifier : UIView], viewController: UIViewController) {
        guard let tadNativeAdView = tadNativeAdView else  { return }
        
        view.subviews.first { $0 is TADNativeAdView }?.removeFromSuperview()
        view.insertSubview(tadNativeAdView, at: 0)
        fillSuperView(view: tadNativeAdView)

        if let titleLabel = clickableAssetViews[GADNativeAssetIdentifier.headlineAsset] as? UILabel {
            titleLabel.isUserInteractionEnabled = true
            tadNativeAdView.longSubjectLabel = titleLabel
        }

        if let bodyLabel = clickableAssetViews[GADNativeAssetIdentifier.bodyAsset] as? UILabel {
            bodyLabel.isUserInteractionEnabled = true
            tadNativeAdView.bodyLabel = bodyLabel
        }

        if let ctaLabel = clickableAssetViews[GADNativeAssetIdentifier.callToActionAsset] {
            ctaLabel.isUserInteractionEnabled = true
            tadNativeAdView.callToActionView = ctaLabel
        }

        if let icon = clickableAssetViews[GADNativeAssetIdentifier.iconAsset] as? UIImageView {
            icon.isUserInteractionEnabled = true
            tadNativeAdView.squareImgeView = icon
        }

        if let mediaView = clickableAssetViews[GADNativeAssetIdentifier.mediaViewAsset], let tadMediaView = tadNativeAdView.mediaView {
            mediaView.subviews.first { $0 is TADNativeMediaView }?.removeFromSuperview()
            mediaView.addSubview(tadMediaView)
            fillSuperView(view: tadMediaView)
        }

        tadNativeAdView.nativeAd = tadNativeAd
    }
    
    func didUntrackView(_ view: UIView?) {
        print("TADMediatedUnifiedNativeAd - didUntrackView")
        tadNativeAdView?.removeFromSuperview()
        tadNativeAdView?.mediaView?.removeFromSuperview()
    }
}

extension TADCustomEventNative {
    func fillSuperView(view: UIView) {
        view.frame = view.superview?.bounds ?? .zero
    }
}

回 AdMob Mediation