檢視 SDK8 AdMob Mediation Native 的原始碼
←
SDK8 AdMob Mediation Native
跳至導覽
跳至搜尋
由於下列原因,您沒有權限進行編輯此頁面的動作:
您請求的操作只有這個群組的使用者能使用:
使用者
您可以檢視並複製此頁面的原始碼。
= 簡介 = 將說明如何將 TAmedia SDK 原生廣告與 Google AdMob 原生廣告([https://developers.google.com/admob/ios/native/advanced Native advanced])做 mediation 整合,關於 AdMob mediation 請參考:https://developers.google.com/admob/ios/mediate = 串接準備 = 首先先完成 mediation 基本 setup [[AdMob Mediation Setup]], TADCustomEvent 將會透過此客製化 class 取得 TADNativeAd。 = 實作方式 = TADCustomEventNative.swift <source> 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 } } </source> [[SDK8 Google AdMob Mediation|回 AdMob Mediation]]
返回到「
SDK8 AdMob Mediation Native
」。
導覽選單
個人工具
登入
命名空間
頁面
討論
變體
已展開
已摺疊
視圖
閱讀
檢視原始碼
檢視歷史
更多
已展開
已摺疊
搜尋
導覽
首頁
近期變更
隨機頁面
有關 MediaWiki 的說明
工具
連結至此的頁面
相關變更
特殊頁面
頁面資訊