📅  最后修改于: 2023-12-03 15:17:43.282000             🧑  作者: Mango
Mopub 是一家移动广告交易平台,提供了各种广告类型和格式,为广告主和开发者提供了灵活的广告展示方式。在集成 Mopub SDK 之前,我们可以使用 Mopub 测试广告来测试广告展示是否正常。
Mopub 测试广告 ID 包括两个部分:广告 unit ID 和广告平台 ID,可以在 Mopub 官网上获取。我们一般使用 Mopub 自带的测试广告 ID 进行测试,具体 ID 如下:
b195f8dd8ded45fe847ad89ed1d016da
20ac0e1861734aada684806d75870816
24534e1901884e398f1253216226017e
20ac0e1861734aada684806d75870816
11a17b188668469fb0412708c3d16813
20ac0e1861734aada684806d75870816
在项目中集成 Mopub SDK 后,我们需要在代码中设置广告 unit ID 和广告平台 ID,以便展示测试广告。
import MoPub
class BannerViewController: UIViewController, MPAdViewDelegate {
var adView: MPAdView!
let adUnitID = "b195f8dd8ded45fe847ad89ed1d016da"
let adSize = CGSize(width: 320, height: 50)
override func viewDidLoad() {
super.viewDidLoad()
adView = MPAdView(adUnitId: adUnitID, size: adSize)
adView.delegate = self
adView.loadAd()
self.view.addSubview(adView)
}
func viewControllerForPresentingModalView() -> UIViewController! {
return self
}
// 实现 MPAdViewDelegate 协议中的方法
// 监听广告加载状态
func adViewDidLoadAd(_ view: MPAdView!) {
print("Banner ad did load")
}
func adViewDidFail(toLoadAd view: MPAdView!) {
print("Banner ad did fail to load")
}
}
import MoPub
class InterstitialViewController: UIViewController, MPInterstitialAdControllerDelegate {
var interstitial: MPInterstitialAdController!
let adUnitID = "24534e1901884e398f1253216226017e"
override func viewDidLoad() {
super.viewDidLoad()
interstitial = MPInterstitialAdController(forAdUnitId: adUnitID)
interstitial.delegate = self
interstitial.loadAd()
}
func viewControllerForPresentingModalView() -> UIViewController! {
return self
}
// 实现 MPInterstitialAdControllerDelegate 协议中的方法
// 监听广告加载状态
func interstitialDidLoadAd(_ interstitial: MPInterstitialAdController!) {
print("Interstitial ad did load")
interstitial.show(from: self)
}
func interstitialDidFail(toLoadAd interstitial: MPInterstitialAdController!) {
print("Interstitial ad did fail to load")
}
func interstitialWillAppear(_ interstitial: MPInterstitialAdController!) {
print("Interstitial ad will appear")
}
func interstitialDidAppear(_ interstitial: MPInterstitialAdController!) {
print("Interstitial ad did appear")
}
func interstitialWillDisappear(_ interstitial: MPInterstitialAdController!) {
print("Interstitial ad will disappear")
}
func interstitialDidDisappear(_ interstitial: MPInterstitialAdController!) {
print("Interstitial ad did disappear")
}
}
import MoPub
class NativeViewController: UIViewController, MPNativeAdDelegate {
var adView: MPNativeAdView!
var nativeAd: MPNativeAd!
let adUnitID = "11a17b188668469fb0412708c3d16813"
override func viewDidLoad() {
super.viewDidLoad()
nativeAd = MPNativeAd(adUnitIdentifier: adUnitID)
nativeAd.delegate = self
nativeAd.loadAd()
adView = MPNativeAdView(nativeAd: nativeAd)
adView.frame = CGRect(x: 0, y: 0, width: 320, height: 250)
self.view.addSubview(adView)
}
// 实现 MPNativeAdDelegate 协议中的方法
// 监听广告加载状态
func nativeAdDidLoad(_ nativeAd: MPNativeAd!) {
print("Native ad did load")
adView.configure(with: nativeAd)
}
func nativeAd(_ nativeAd: MPNativeAd!, didFailToLoadWithError error: Error!) {
print("Native ad did fail to load")
}
}
在集成并展示 Mopub 测试广告后,我们可以通过以下方式来测试广告效果。
以上就是 Mopub 测试广告的介绍和集成示例。在实际应用中,我们需要根据实际情况调整广告类型、展示位置等相关参数,以达到最佳的广告效果。