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

1k Views Asked by At

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. 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. 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.

In my AppDelegate.swift I have the following code to prepare the iAd video.

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...

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")
            }
        })
1

There are 1 best solutions below

0
On

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.

Just download AdMob and put in in your app. 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!) {

}