简体   繁体   English

如何在Swift和iOS 9中实施iAd预卷视频?

[英]How do you implement iAd Preroll Video in Swift and iOS 9?

I'm trying to play a Preroll iAd Video into my game without having a video play afterward which is how I see it's done in all of the examples I've seen. 我试图在不播放视频的情况下将Preroll iAd视频播放到我的游戏中,这是我所看到的所有示例中看到的效果。 When I go to play the Preroll iAd the AV View Controller is displayed but nothing plays and I get into my error case as shown below saying it couldn't play. 当我去播放Preroll iAd时,会显示AV View Controller,但是什么也没有播放,并且出现如下所示的错误情况,说无法播放。 What I'm going for is done in games like "Pop The Lock" when it gives you a second chance if you watch a video ad. 我想要的是在“ Pop The Lock”之类的游戏中完成的,如果您观看视频广告会给您第二次机会。

In my AppDelegate.swift I have the following code to prepare the iAd video. 在我的AppDelegate.swift中,我具有以下代码来准备iAd视频。

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool 
{

        // Override point for customization after application launch.
        AVPlayerViewController.preparePrerollAds()
        return true
}

Then in my ViewController I have the following to play the ad... 然后在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")
            }
        })

I wanted to do the same thing and was unsuccessful with pre-roll video. 我想做同样的事情,但前贴片视频没有成功。 Now I have iAd for my bannerView and i use AdMob for the video advertisement to get a second chance. 现在,我的bannerView使用iAd,我将AdMob用于视频广告来获得第二次机会。

Just download AdMob and put in in your app. 只需下载AdMob并放入您的应用中即可。 Then import 然后导入

import GoogleMobileAds

create a variable 创建一个变量

var interstitial: GADInterstitial!

Then create the func that will load and show the video ad. 然后创建将加载并显示视频广告的功能。

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!) {

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM