问题描述
我有一个 SwiftUI/SpriteKit 项目,我正在尝试使用找到的测试广告 ID here 来实现 MoPub 插页式广告。
广告按预期加载,点击广告即可成功,但广告上没有关闭按钮。
我已经通过 CocoaPods 集成了 MoPub SDK,如下所示:
pod 'mopub-ios-sdk'
我正在 AppDelegate
的 application(_:didFinishLaunchingWithOptions:)
函数中初始化 MoPub SDK,如下所示:
let sdkConfig = MPMoPubConfiguration(adUnitIdForAppInitialization: "MyIdHere")
sdkConfig.loggingLevel = .debug
MoPub.sharedInstance().initializeSdk(with: sdkConfig,completion: {
dispatchQueue.main.async {
//The SDK is initialized.
}
})
我通过下面的 showAd
函数展示广告:
class InterstitialAds: UIViewController,MPAdViewDelegate,MPInterstitialAdControllerDelegate {
var moPubView: MPAdView?
func viewControllerForPresentingModalView() -> UIViewController! {
return self
}
func interstitialDidLoadAd(_ interstitial: MPInterstitialAdController) {
}
func interstitialDidFail(toLoadAd: MPInterstitialAdController,withError: Error){
}
func interstitialWilldismiss(_ interstitial: MPInterstitialAdController) {
}
func interstitialDiddismiss(_ interstitial: MPInterstitialAdController) {
}
func interstitialDidExpire(_ interstitial: MPInterstitialAdController) {
}
func showAd() {
let topViewController = UIApplication.shared.windows.filter {$0.isKeyWindow}.first?.rootViewController
self.modalPresentationStyle = .fullScreen
topViewController?.present(self,animated: true) {
let adId = "4f117153f5c24fa6a3a92b818a5eb630" //Test ad unit id
self.moPubView = MPAdView.init(adUnitId: adId)
let bounds = self.view.bounds
var adFrame = CGRect.zero
adFrame.size = CGSize(width: UIScreen.main.bounds.width,height: UIScreen.main.bounds.height)
if let v = self.moPubView {
v.frame = adFrame
v.maxAdSize = kMPPresetMaxAdSizeMatchFrame
v.delegate = self
self.view.addSubview(v)
v.loadAd()
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
谢谢!
解决方法
我通过进行一些更改设法使其正常工作:
- 将
MPAdView
替换为MPInterstitialAdController
。 - 将
MPAdView.init(adUnitId: adId)
替换为MPInterstitialAdController(forAdUnitId: adId)
。 - 在调用
show(from:)
后调用了loadAd()
关闭按钮现在会根据需要显示。