在我的应用程序中集成 iAd 前置视频集成?

Integrate iAd pre-roll video integration in my app?

我想将 iAd Pre-Roll 视频广告集成到我的应用程序中。当我 运行 这个应用程序时,它给我这个错误:

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

我想知道这个代码是正确的还是不正确的。感谢您的帮助。

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


}

这是我修改的 App Delegate 函数之一:

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

您试图在您的应用程序有时间下载它之前显示您的前置视频广告。几秒钟后触发您的 moviePlayer.playPrerollAdWithCompletionHandler 或将视频移到介绍中的稍后位置,以便您的应用程序有时间下载广告。检查我的例子:

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()
        }
    }

在应用程序启动几秒钟后点击 UIButton playVideoButton 将播放预播放的视频广告,然后是所需的视频。

此外,如果您在设备上进行测试,请转到“设置”>“开发人员”>“填充率”> 并确保将其设置为 100%。