我正在尝试在我的游戏中播放 Preroll iAd 视频,之后不播放视频,这就是我看到的所有示例中的做法。当我去播放 Preroll iAd 时,会显示 AV View Controller 但没有播放任何内容,我进入了我的错误案例,如下所示,说它无法播放。我想要的是在“Pop The Lock”这样的游戏中完成的,当你观看视频广告时它会给你第二次机会。
在我的 AppDelegate.swift 中,我有以下代码来准备 iAd 视频。
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
// Override point for customization after application launch.
AVPlayerViewController.preparePrerollAds()
return true
}
然后在我的 ViewController 中我有以下内容来播放广告...
import AVFoundation
import iAd
//this is declared at the top
let adPlayerController = AVPlayerViewController()
//this gets called inside of a function
adPlayerController.playPrerollAdWithCompletionHandler({ (error) -> Void in
if error != nil
{
print(error)
print("Ad could not be loaded")
}
else
{
print("Ad loaded")
}
})
最佳答案
我想做同样的事情,但没有成功使用前置视频。现在我的 bannerView 有 iAd,我使用 AdMob 做视频广告以获得第二次机会。
只需下载 AdMob 并将其放入您的应用中即可。然后导入
import GoogleMobileAds
创建一个变量
var interstitial: GADInterstitial!
然后创建将加载和显示视频广告的函数。
func loadAd() {
self.interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
let request = GADRequest()
// Requests test ads on test devices.
request.testDevices = ["e23db0f2cf8d82b7b4f23ede7df2f928"]
interstitial.delegate = self
self.interstitial.loadRequest(request)
}
func showAd() {
if self.interstitial.isReady {
let vc = self.view!.window?.rootViewController
self.interstitial.presentFromRootViewController(vc)
}
}
func interstitial(ad: GADInterstitial!, didFailToReceiveAdWithError error: GADRequestError!) {
print("Video Ad did not load")
}
func interstitialDidDismissScreen(ad: GADInterstitial!) {
}
func interstitialDidReceiveAd(ad: GADInterstitial!) {
}
func interstitialWillLeaveApplication(ad: GADInterstitial!) {
}
func interstitialWillPresentScreen(ad: GADInterstitial!) {
}
func interstitialWillDismissScreen(ad: GADInterstitial!) {
}
关于swift - 如何在 Swift 和 iOS 9 中实现 iAd Preroll Video?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34794936/