動画リワード広告

動画広告の視聴完了時にユーザーにインセンティブを付与することができます。

1. 動画広告の初期化を行う

AdstirVideoAds prepareWithMediaを使い、プロジェクトで利用する全ての動画広告の初期化を同時に行います。

動画リワード広告と全画面インタースティシャル広告を併用する場合は、動画リワード広告と全画面インタースティシャル広告の広告枠全て同時に初期化を行う必要があります。

[AdstirVideoAds prepareWithMedia:@"メディアID" spots:@[@No1, @No2]];
AdstirVideoAds.prepare(withMedia: "メディアID", spots: [1, 2]);

2. 動画リワード広告のインスタンスを生成

AdstirVideoRewardのインスタンスを生成します。

AdstirVideoReward *aVideoReward = [[AdstirVideoReward alloc] initWithMedia:@"メディアID" spot:No];
let videoReward = AdstirVideoReward(media: "メディアID", spot: No)!

3. デリゲートの実装(オプション)

- (void)viewDidLoad
{
    [super viewDidLoad];
        :
    aVideoReward.delegate = self;
        :
}

/** 動画の読み込み完了イベントを受け取ります */
- (void)adstirVideoRewardDidLoad:(AdstirVideoReward *)videoReward
{
}

/** 動画の再生準備に失敗した際に呼び出されます */
- (void)adstirVideoReward:(AdstirVideoReward *)videoReward didFailToLoadWithError:(NSError *)error
{
}

/** 動画の再生開始イベントを受け取ります */
- (void)adstirVideoRewardDidStart:(AdstirVideoReward *)videoReward
{
}

/** 動画のインセンティブ付与の完了イベントを受け取ります */
- (void)adstirVideoRewardDidComplete:(AdstirVideoReward *)videoReward
{
}

/** 動画再生のキャンセルイベントを受け取ります */
- (void)adstirVideoRewardDidCancel:(AdstirVideoReward *)videoReward
{
}

/** 動画再生ビューが閉じられたことを受け取ります */
- (void)adstirVideoRewardDidClose:(AdstirVideoReward *)videoReward
{
}
override func viewDidLoad() {
    ...
    let videoReward = AdstirVideoReward.init(media: "メディアID", spot: No)
    videoReward?.delegate = self;
    ...
}

 /** 動画の読み込み完了イベントを受け取ります */
func adstirVideoRewardDidLoad(_ videoReward: AdstirVideoReward) {
}

/** 動画の再生準備に失敗した際に呼び出されます */
func adstirVideoReward(_ videoReward: AdstirVideoReward, didFailToLoadWithError error: NSError) {
}

/** 動画の再生開始イベントを受け取ります */
func adstirVideoRewardDidStart(_ videoReward: AdstirVideoReward) {
}

/** 動画のインセンティブ付与の完了イベントを受け取ります */
func adstirVideoRewardDidComplete(_ videoReward: AdstirVideoReward) {
}

/** 動画再生のキャンセルイベントを受け取ります */
func adstirVideoRewardDidCancel(_ videoReward: AdstirVideoReward) {
}

/** 動画再生ビューが閉じられたことを受け取ります */
func adstirVideoRewardDidClose(_ videoReward: AdstirVideoReward) {
}

4. 動画リワード広告の読み込み

動画リワード広告の読み込みを行います。

[aVideoReward load];
videoReward.load()

5. 動画リワード広告の再生

読み込みが完了した動画リワード広告を再生します。 動画の再生を行うViewControllerを渡してください。 動画の再生後、もう一度動画を再生するためには5.動画リワード広告の読み込みを行う必要があります。

if ([aVideoReward canShow]) {
    [aVideoReward showFromViewController:self]];
}
if (videoReward.canShow()) {
    videoReward.show(from: self);
}

SDKの実装例

ここでは、単純な実装についてのサンプルを提示します。 まず、Storyboard上に、UIButtonUILabelを配置し、UIButtonをのOutletとActionをshowVideoButtonshowVideoButtonDidTouchUpInsideにそれぞれ接続し、UILabelのOutletをstatusLabelに接続してください。

  • AppDelegate
@import AdstirAds;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    /* 他の初期化コード */

    // メディアIDと、このアプリ内で利用する動画インセンティブメニューの枠IDとインタースティシャル広告の枠IDをすべて指定します。
    // 動画リワードとインタースティシャルを併用する場合はどちらの枠Noも指定してください。
    [AdstirVideoAds prepareWithMedia:@"メディアID" spots:@[@No, @No]];

    return YES;
}
// ...
import AdstirAds
// ...
class AppDelegate: UIResponder, UIApplicationDelegate {
// ...
   func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        /* 他の初期化コード */

        // メディアIDと、このアプリ内で利用する動画インセンティブメニューの枠IDとインタースティシャル広告の枠IDをすべて指定します。
        // 動画リワードとインタースティシャルを併用する場合はどちらの枠Noも指定してください。
        AdstirVideoAds.prepare(withMedia:"メディアID", spots: [No, No])
        return true
    }
// ...
}
  • ViewController
@import AdstirAds;

@interface FooViewController () <AdstirVideoRewardDelegate>

@property (weak, nonatomic) IBOutlet UILabel *statusLabel;
@property (weak, nonatomic) IBOutlet UIButton *showVideoButton;

@property (nonatomic, strong) AdstirVideoReward *videoReward;

@property (nonatomic, assign) BOOL isVideoRewardCompleted;

@end

@implementation FooViewController

- (void)viewDidLoad
{
    AdstirVideoReward *aVideoReward = [[AdstirVideoReward alloc] initWithMedia:@"メディアID" spot:No];
    aVideoReward.delegate = self;

    self.videoReward = aVideoReward;

    self.statusLabel.text = @"Loading...";
    [self.videoReward load];
}

/** リワード広告を表示するボタンのターゲットメソッド */
- (IBAction)showVideoButtonDidTouchUpInside:(id)sender
{
    self.showVideoButton.enabled = NO;
    if ([self.videoReward canShow]) {
        [self.videoReward showFromViewController:self]];
    }
}

- (void)dealloc
{
    // デリゲートを解放します。解放を忘れるとクラッシュする可能性があります。
    self.videoReward.delegate = nil;
    // 広告の制御インスタンスを解放します。
    self.videoReward = nil;
}

#pragma mark -
#pragma mark AdstirVideoRewardDelegate

/** 動画の読み込み完了イベントを受け取ります */
- (void)adstirVideoRewardDidLoad:(AdstirVideoReward *)videoReward
{
    self.statusLabel.text = @"Loaded.";
    self.showVideoButton.enabled = YES;
}

/**
 動画の再生準備に失敗した際に呼び出されます
 */
- (void)adstirVideoReward:(AdstirVideoReward *)videoReward didFailToLoadWithError:(NSError *)error
{
    NSLog(@"動画が読み込めませんでした");
    // 15秒待ってから再読み込みします
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(15.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.statusLabel.text = @"Loading...";
        [self.videoReward load];
    });
}

/** 動画の再生開始イベントを受け取ります */
- (void)adstirVideoRewardDidStart:(AdstirVideoReward *)videoReward
{
    self.statusLabel.text = @"";
}

/** 動画のインセンティブ付与の完了イベントを受け取ります */
- (void)adstirVideoRewardDidComplete:(AdstirVideoReward *)videoReward
{
    self.isVideoRewardCompleted = YES;
}

/** 動画再生のキャンセルイベントを受け取ります */
- (void)adstirVideoRewardDidCancel:(AdstirVideoReward *)videoReward
{
    // 15秒待ってから再読み込みします
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(15.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.statusLabel.text = @"Loading...";
        [self.videoReward load];
    });
}

/** 動画再生ビューが閉じられたことを受け取ります */
- (void)adstirVideoRewardDidClose:(AdstirVideoReward *)videoReward
{
    if (self.isVideoRewardCompleted) {
        self.statusLabel.text = @"Congrats! Got virtual currency!";
    }
    self.isVideoRewardCompleted = NO;

    // 15秒待ってから再読み込みします
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(15.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.statusLabel.text = @"Loading...";
        [self.videoReward load];
    });
}

@end
import UIKit
import AdstirAds

class ViewController: UIViewController, AdstirVideoRewardDelegate {

    @IBOutlet weak var statusLabel: UILabel!
    @IBOutlet weak var showVideoButton: UIButton!

    var videoReward: AdstirVideoReward?
    var isVideoRewardCompleted = false

    override func viewDidLoad() {
        super.viewDidLoad()

        self.showVideoButton.enabled = false

        let videoReward = AdstirVideoReward(media: "メディアID", spot: No)
        videoReward?.delegate = self;
        self.videoReward = videoReward;

        self.videoReward?.load()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    /** リワード広告を表示するボタンのターゲットメソッド */
    @IBAction func showVideoButtonTouchUpInside(sender: AnyObject) {
        self.showVideoButton.enabled = false
        self.videoReward?.show(from: self);
    }

    /** 動画の読み込み完了イベントを受け取ります */
    func adstirVideoRewardDidLoad(_ videoReward: AdstirVideoReward) {
        self.statusLabel.text = "Loaded"
        self.showVideoButton.enabled = true
    }

    /** 動画の再生準備に失敗した際に呼び出されます */
    func adstirVideoReward(_ videoReward: AdstirVideoReward, didFailToLoadWithError error: Error) {
        NSLog("動画が読み込めませんでした")

        // 15秒待ってから再読み込みします
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (Int64)(15.0f * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), {
            self.statusLabel.text = "Loading...";
            self.videoReward?.load()
        })
    }

    /** 動画の再生開始イベントを受け取ります */
    func adstirVideoRewardDidStart(_ videoReward: AdstirVideoReward) {
        self.statusLabel.text = ""
    }

    /** 動画のインセンティブ付与の完了イベントを受け取ります */
    func adstirVideoRewardDidComplete(_ videoReward: AdstirVideoReward) {
        self.isVideoRewardCompleted = true
    }

    /** 動画再生のキャンセルイベントを受け取ります */
    func adstirVideoRewardDidCancel(_ videoReward: AdstirVideoReward) {
        // 15秒待ってから再読み込みします
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (Int64)(15.0f * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), {
            self.statusLabel.text = "Loading...";
            self.videoReward?.load()
        })
    }

    func adstirVideoRewardDidClose(_ videoReward: AdstirVideoReward) {
        if (self.isVideoRewardCompleted) {
            self.statusLabel.text = "Congrats! Got virtual currency!"
        }
        self.isVideoRewardCompleted = false

        // 15秒待ってから再読み込みします
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (Int64)(15.0f * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), {
            self.statusLabel.text = "Loading...";
            self.videoReward?.load()
        })
    }

    deinit {
        // デリゲートを解放します。解放を忘れるとクラッシュする可能性があります。
        self.videoReward?.delegate = nil;
        // 広告の制御インスタンスを解放します。
        self.videoReward = nil;
    }
}