「SDK8 AdMob Mediation Native」修訂間的差異
跳至導覽
跳至搜尋
CharlesWang(留言 | 貢獻) |
CharlesWang(留言 | 貢獻) |
||
(未顯示同一使用者於中間所作的 4 次修訂) | |||
行 3: | 行 3: | ||
= 串接準備 = | = 串接準備 = | ||
首先先完成 mediation 基本 setup [[AdMob Mediation Setup]], TADCustomEvent 將會透過此客製化 class 取得 TADNativeAd。 | |||
= 實作方式 = | |||
TADCustomEventNative.swift | |||
<source> | |||
import Foundation | import Foundation | ||
import GoogleMobileAds | import GoogleMobileAds | ||
import TAMediaAdsFramework | import TAMediaAdsFramework | ||
class | |||
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 { | deinit { | ||
print(" | print("TADCustomEventNative - TADMediatedUnifiedNativeAd deinit") | ||
} | } | ||
func | func loadNativeAd(for adConfiguration: GADMediationNativeAdConfiguration, | ||
completionHandler: @escaping GADMediationNativeLoadCompletionHandler) { | |||
self.completionHandler = completionHandler | |||
let request = TADRequest() | let request = TADRequest() | ||
request.showLog = true | request.showLog = true | ||
let option = TADNativeAdOptions() | let option = TADNativeAdOptions() | ||
///參見 http://wiki.tamedia.com.tw/iosDoc/Classes/TADNativeAdOptions.html | /// 參見 http://wiki.tamedia.com.tw/iosDoc/Classes/TADNativeAdOptions.html | ||
// option.disableImageLoading = true //SDK 將不自動下載圖片 | // option.disableImageLoading = true //SDK 將不自動下載圖片 | ||
// option.mediaPreferImage = | // option.mediaPreferImage = true //只會呈現圖片廣告 | ||
// option.startUnmuted = true //影片廣告會以非靜音方式呈現 | // option.startUnmuted = true //影片廣告會以非靜音方式呈現 | ||
// option.customControlsRequested = true // | // option.customControlsRequested = true //自行定義影音廣告的靜音/取消靜音、倒計時標籤和了解更多按鈕 | ||
// option.allowAudioSessionControl = false //設為false則需自行設定影音廣告出現時 AVAudioSession 狀態 | // 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 | extension TADCustomEventNative: TADNativeAdDelegate { | ||
func nativeAdDidReceive(_ ad: TADNativeAd!) { | func nativeAdDidReceive(_ ad: TADNativeAd!) { | ||
let nativeAdView = TADNativeAdView() | let nativeAdView = TADNativeAdView() | ||
let mediaView = TADNativeMediaView(frame: .zero) | let mediaView = TADNativeMediaView(frame: .zero) | ||
mediaView.mediaContent = ad.adContent.mediaContent | mediaView.mediaContent = ad.adContent.mediaContent | ||
///參見 http://wiki.tamedia.com.tw/iosDoc/Classes/TADNativeMediaView.html | /// 參見 http://wiki.tamedia.com.tw/iosDoc/Classes/TADNativeMediaView.html | ||
// mediaView.setCallToActionVisible(false) //是否顯示 CTA 按鈕 | // mediaView.setCallToActionVisible(false) //是否顯示 CTA 按鈕 | ||
// mediaView.setCallToActionTextSize(12) //設定 CTA 文字大小 | // mediaView.setCallToActionTextSize(12) //設定 CTA 文字大小 | ||
行 73: | 行 78: | ||
nativeAdView.mediaView = mediaView | nativeAdView.mediaView = mediaView | ||
setup(nativeAd: ad, nativeAdView: nativeAdView) | |||
delegate | |||
if let handler = completionHandler { | |||
delegate = handler(self, nil) | |||
} | |||
} | } | ||
func nativeAd(_ ad: TADNativeAd!, didFailToReceiveAdWithError error: TADRequestError!) { | func nativeAd(_ ad: TADNativeAd!, didFailToReceiveAdWithError error: TADRequestError!) { | ||
if let handler = completionHandler { | |||
delegate = handler(nil, error) | |||
if let | |||
} | } | ||
} | } | ||
func nativeAdDidImpression(_ ad: TADNativeAd!) { | func nativeAdDidImpression(_ ad: TADNativeAd!) { | ||
print("TADCustomEventNative - nativeAdDidImpression") | |||
print(" | delegate?.reportImpression() | ||
} | } | ||
func nativeAdDidClick(_ ad: TADNativeAd!) { | func nativeAdDidClick(_ ad: TADNativeAd!) { | ||
print("TADCustomEventNative - nativeAdDidClick") | |||
print(" | delegate?.reportClick() | ||
} | } | ||
} | } | ||
extension | extension TADCustomEventNative: TADVideoStatusDelegate { | ||
func didStartVideo() { | func didStartVideo() { | ||
delegate?.didPlayVideo() | |||
} | } | ||
func didPlayVideo() { | func didPlayVideo() { | ||
delegate?.didPlayVideo() | |||
} | } | ||
func didPauseVideo() { | func didPauseVideo() { | ||
delegate?.didPauseVideo() | |||
} | } | ||
func didEndVideoPlayback() { | func didEndVideoPlayback() { | ||
delegate?.didEndVideo() | |||
} | } | ||
func didMuteVideo() {} | func didMuteVideo() { | ||
delegate?.didMuteVideo() | |||
} | |||
func didUnmuteVideo() {} | func didUnmuteVideo() { | ||
delegate?.didUnmuteVideo() | |||
} | |||
} | } | ||
extension | extension TADCustomEventNative: GADMediationNativeAd { | ||
var headline: String? { | var headline: String? { | ||
return tadNativeAd.adContent.longSubject | return tadNativeAd?.adContent.longSubject | ||
} | } | ||
行 164: | 行 139: | ||
var body: String? { | var body: String? { | ||
return tadNativeAd.adContent.body | return tadNativeAd?.adContent.body | ||
} | } | ||
行 172: | 行 147: | ||
var callToAction: String? { | var callToAction: String? { | ||
return tadNativeAd.adContent.callToAction | return tadNativeAd?.adContent.callToAction | ||
} | } | ||
行 196: | 行 171: | ||
var mediaView: UIView? { | var mediaView: UIView? { | ||
return tadNativeAdView.mediaView | return tadNativeAdView?.mediaView | ||
} | } | ||
var hasVideoContent: Bool { | var hasVideoContent: Bool { | ||
guard let mediaContent = tadNativeAd.adContent.mediaContent else { | guard let mediaContent = tadNativeAd?.adContent.mediaContent else { | ||
return false | return false | ||
} | } | ||
行 206: | 行 181: | ||
} | } | ||
func didRender(in view: UIView, clickableAssetViews: [GADNativeAssetIdentifier: UIView], nonclickableAssetViews: [GADNativeAssetIdentifier: UIView], viewController: UIViewController) { | 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.subviews.first { $0 is TADNativeAdView }?.removeFromSuperview() | ||
view.insertSubview(tadNativeAdView, at: 0) | view.insertSubview(tadNativeAdView, at: 0) | ||
fillSuperView(view: tadNativeAdView) | fillSuperView(view: tadNativeAdView) | ||
if let titleLabel = clickableAssetViews[GADNativeAssetIdentifier.headlineAsset] as? UILabel { | if let titleLabel = clickableAssetViews[GADNativeAssetIdentifier.headlineAsset] as? UILabel { | ||
titleLabel.isUserInteractionEnabled = true | titleLabel.isUserInteractionEnabled = true | ||
行 225: | 行 210: | ||
tadNativeAdView.callToActionView = ctaLabel | tadNativeAdView.callToActionView = ctaLabel | ||
} | } | ||
if let icon = clickableAssetViews[GADNativeAssetIdentifier.iconAsset] as? UIImageView { | if let icon = clickableAssetViews[GADNativeAssetIdentifier.iconAsset] as? UIImageView { | ||
icon.isUserInteractionEnabled = true | icon.isUserInteractionEnabled = true | ||
行 236: | 行 221: | ||
fillSuperView(view: tadMediaView) | fillSuperView(view: tadMediaView) | ||
} | } | ||
tadNativeAdView.nativeAd = tadNativeAd | tadNativeAdView.nativeAd = tadNativeAd | ||
} | } | ||
行 242: | 行 227: | ||
func didUntrackView(_ view: UIView?) { | func didUntrackView(_ view: UIView?) { | ||
print("TADMediatedUnifiedNativeAd - didUntrackView") | print("TADMediatedUnifiedNativeAd - didUntrackView") | ||
tadNativeAdView.removeFromSuperview() | tadNativeAdView?.removeFromSuperview() | ||
tadNativeAdView.mediaView?.removeFromSuperview() | tadNativeAdView?.mediaView?.removeFromSuperview() | ||
} | } | ||
} | } | ||
extension | extension TADCustomEventNative { | ||
func fillSuperView(view: UIView) { | func fillSuperView(view: UIView) { | ||
view.frame = view.superview?.bounds ?? .zero | view.frame = view.superview?.bounds ?? .zero | ||
} | } | ||
} | } | ||
</ | </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
}
}