6. Video Ad‎ > ‎

2) iOS

Before starting this chapter, be sure that you already finished SDK settings. If not, check 4) iOS Settings first.

For video Ad, first you have to load a video Ad by invoking prepareVideoAd: method on an instance of TnkSession and showVdieoAd: to play it. Once you have invoked prepareVideoAd: method, next video Ads are being loaded automatically at background task.

[Loading a Video Ad]

To load a video Ad, you have to invoke prepareVideoAd: method on the instance of TnkSession. First parameter is the name of Display Logic. (See 5) Display Logic for detail) and second parameter is an instance of TnkVideoDelegate which receives events of video loadings.

Prepare a Video Ad

@interface ViewController : UIViewController <TnkVideoDelegate


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    [[TnkSession sharedInstance] prepareVideoAd:@"your_display_logic_name" delegate:self];


}


@end


[TnkVideoDelegate]

To get events from video Ads, you can put an instance of TnkVideoDelegate protocol in the second parameter of prepareVideoAd: method.

TnkVideoDelegate Protocol

#define AD_CLOSE_SIMPLE     0

#define AD_CLOSE_CLICK      1


@protocol TnkVideoDelegate <NSObject>

@optional


// Invoked when a video ad has been successfully loaded.

- (void)didVideoLoad:(NSString *)logicName;


// Invoked when a video ad is shown up

- (void)didVideoShow:(NSString *)logicName;


// Invoked when user completed watching the video ad.

// If user click skip button while watching video, true is passed in the parameter.

- (void)didVideoCompleted:(NSString *)logicName skip:(BOOL)skipped;


// Invoked when video Ad view is closed. Reason for closing view is passed in type parameter.

// 0 for closing view by clicking close button, and 1 for closing view by clicking download button.

- (void)didVideoClose:(NSString *)logicName close:(int)type;


// Invoked when a video has failed to load. This event is only used when you put repeat:NO parameter in prepareVideoAd: API.

// If you put repeat:YES(default) in prepareVideoAd: API, this event is never invoked even if a video is failed to load. In this case, SDK repeat loading a video in background.

- (void)didVideoFail:(NSString *)logicName error:(int)errCode;

@end


[Video Ad Sample]
In this sample , a 'Play' button is shown when a video ad is loaded and the video is played when user clicks the button.

Video Ad Sample

#import "tnksdk.h"


@interface ViewController : UIViewController <TnkVideoDelegate>


@property (nonatomic, weak) IBOutlet UIButton *videoButton;

- (IBAction)videoAdPressed:(id)sender;


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];


    self.videoButton.hidden = YES;

    [[TnkSession sharedInstance] prepareVideoAd:@"intro_video" delegate:self];

}


- (IBAction)videoAdPressed:(id)sender {

    

    if ([[TnkSession sharedInstance] hasVideoAd:@"intro_video"]) {

        // hide 'Play' button on clicking the 'Play' button.

        [[TnkSession sharedInstance] showVideoAd:@"intro_video" on:self];

        

        self.videoButton.hidden = YES;

    }

    else {

        // No video Ad

    }

}


#pragma mark - TnkVideoDelegate


- (void) didVideoLoad:(NSString *)logicName {

    // show 'Play' button

    self.videoButton.hidden = NO;

}


- (void) didVideoShow:(NSString *)logicName {

    NSLog(@"### didVideoShow");

}


- (void) didVideoClose:(NSString *)logicName close:(int)type {

    NSLog(@"### didVideoClosed");

}


- (void) didVideoCompleted:(NSString *)logicName skip:(BOOL)skipped {

    NSLog(@"### didVideoCompleted");

}


@end



[Portrait-oriented App]
Video Ad should be played only in landscape orientation. You have to add following code in your AppDelegate class to support landscape play of video ad if your app uses only portrait orientation.

AppDelegate.m

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow*)window

{

    if ([[TnkSession sharedInstanceisVideoPresenting]) {

        return UIInterfaceOrientationMaskLandscape;

    }

    else {

        return UIInterfaceOrientationMaskPortrait;

    }

}


[Video Ad APIs]

- TnkSession - prepareVideoAd:

Method

  - (void) prepareVideoAd:(NSString *)logicName delegate:(id<TnkVideoDelegate>)delegate;

  - (void) prepareVideoAd:(NSString *)logicName delegate:(id<TnkVideoDelegate>)delegate repeat:(BOOL)repeatFlag;
Description
   Load a video Ad. Once the video ad is loaded. you can play it by invoking showVideoAd: method.

Parameters
 Name Description
 logicName Name of Display Logic. See 5) Display Logic for detail.
 delegate instance of TnkVideoDelegate
 repeatFlag YES is default value. NO used when you want to load a video once.

- TnkSession - showVideoAd:

Method

  - (BOOL) showVideoAd:(NSString *)logicName on:(UIViewController *)viewController;


Description
   Play a video ad loaded by prepareVideoAd: method.

Parameters
 Name Description
 logicName Name of Display Logic. Set nil if you want to play any video Ad regardless of Display Logic.
 viewControllera ViewController on which a video ad is played. if not assigned, rootViewController of current UIWindow is used.

- TnkSession - hasVideoAd:

Method

  - (BOOL) hasVideoAd:(NSString *)logicName;


Description
  Returns YES if a video ad is loaded and ready for playing.

Parameters
 Name Description
 logicName Name of Display logic. Set nil if you want to check any video ad regardless of Display Logic.
Comments