Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

swift - Integrate iAd pre-roll video integration in my app?

I want to integrate an iAd Pre-Roll video Ad to my application. When I run this application, it gives me this error:

Domain=ADErrorDomain Code=0 "The operation couldn’t be completed. (ADErrorDomain error 0.)

I want to know if this code is correct or incorrect. Thanks for your help.

import UIKit
import MediaPlayer
import iAd

class ViewController: UIViewController {

    var moviePlayer : MPMoviePlayerController!

    override func viewDidLoad() {
        super.viewDidLoad()




        let url = NSBundle.mainBundle().URLForResource("intro", withExtension: "mp4")
        moviePlayer = MPMoviePlayerController(contentURL: url)

        moviePlayer!.view.frame = view.frame

        moviePlayer!.prepareToPlay()
        view.addSubview(moviePlayer!.view!)


        moviePlayer.playPrerollAdWithCompletionHandler { (error) -> Void in
            NSLog("(error)")
            self.moviePlayer.play()
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

This is one of my modified App Delegate function:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        MPMoviePlayerController.preparePrerollAds()
        return true
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You're trying to display your Pre-Roll Video Ad before your application has had any time to download it. Fire your moviePlayer.playPrerollAdWithCompletionHandler after a few seconds or move the video to a later point in your intro so your application has time to download the ad. Check my example:

import UIKit
import MediaPlayer
import iAd

class ViewController: UIViewController {
    // Create our MPMoviePlayerController
    var moviePlayer = MPMoviePlayerController()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Preload ad
        MPMoviePlayerController.preparePrerollAds()

        // Setup our MPMoviePlayerController
        moviePlayer.view.frame = self.view.bounds
        moviePlayer.setFullscreen(true, animated: true)
    }

    @IBAction func playVideoButton(sender: AnyObject) {
        // Add our MPMoviePlayerController to our view
        self.view.addSubview(moviePlayer.view)

        // Path of video you want to play
        let videoURL = NSBundle.mainBundle().URLForResource("videoName", withExtension:"MOV")

        // Set the contents of our MPMoviePlayerController to our video path
        moviePlayer.contentURL = videoURL

        // Prepare our movie for playback
        moviePlayer.prepareToPlay()

        // Play our video with a prerolled ad
        moviePlayer.playPrerollAdWithCompletionHandler { (error) -> Void in
            if (error) != nil {
                NSLog("(error)")
            }
            self.moviePlayer.play()
        }
    }

Tapping the UIButton playVideoButton a few seconds after application launch will play the prerolled video advertisement and then the desired video.

Also, If you're testing on your device go to Settings>Developer>Fill Rate> and make sure it is set to 100%.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...