5. Native Ad‎ > ‎

2) iOS

Before starting this chapter, you have to got finished SDK settings. If not, check 4) iOS Settings first.

[TnkNativeAd Loading]

To load a native Ad with a cover image in landscape, just invoke prepare: method as the sample code.

NativeAdItem

#import "tnksdk.h"


@interface ViewController : UIViewController <TnkNativeAdDelegate> {

    // ...

}

@end


@implementation ViewController {

    TnkNativeAd *tnkNative;

}


- (void)viewDidLoad {

    [super viewDidLoad];

    

    // 1) Init Native Ad instance.

    tnkNative = [[TnkNativeAd alloc] init];

    tnkNative.adStyle = AD_STYLE_LANDSCAPE// Cover image in landscape.

    

    // 2) load a native ad

    [tnkNative prepare:@"cross_first" delegate:self];

}


#pragma mark - TnkNativeAdDelegate


- (void) didNativeAdLoad:(TnkNativeAd *)ad {

    NSLog(@"Native Ad Loaded : %@", ad.logicName);

    

    // Rendering native Ad

    

}


[Display NativeAd]

Once loading a native ad is completed. didNativeAdLoad: in TnkNativeAdDelegate is invoked with a parameter of TnkNativeAd which contains data for native Ad.
You can render the native Ad with the data in the parameter.

Rendering Native Ad

#pragma mark - TnkNativeAdDelegate


- (void) didNativeAdLoad:(TnkNativeAd *)ad {

    NSLog(@"Native Ad Loaded : %@", ad.logicName);

    

    // Rendering native Ad

    [self.coverImageView setImage:[ad getCoverImage]];

    [self.iconImageView setImage:[ad getIconImage]];

    [self.titleLabel setText:[ad getTitle]];

    [self.descLabel setText:[ad getDescription]];

    

    [ad attachLayout:self.coverImageView];

}


When the native Ad is displayed in your app, you should call attachLayout: method of TnkNativeAd with the parameter of the UIView which the native Ad is displayed in.
SDK will handle click event in automatic with the UIView you passed in the parameter

When you remove the view for native ad from the display. you should invoke detachLayout: method. Once detachLayout: method is invoked, the instance of TnkNativeAd can be reused to load another Native Ad.

[Handle clicks on NativeAd]

There are 2 ways to handle click on Native ads. First one is to assign a view to click by passing the view when invoking attachLayout:clickView: method. Once you assigned the view to click, SDK automatically handles click events on the view. Second one is to invoke handleClick method when users click a view.

Native Ad Click #1

#pragma mark - TnkNativeAdDelegate


- (void) didNativeAdLoad:(TnkNativeAd *)ad {

    NSLog(@"Native Ad Loaded : %@", ad.logicName);

    

    // Rendering the native Ad

    [self.coverImageView setImage:[ad getCoverImage]];

    [self.iconImageView setImage:[ad getIconImage]];

    [self.titleLabel setText:[ad getTitle]];

    [self.descLabel setText:[ad getDescription]];

    

    // AttachLayout with a UIView to click

    [ad attachLayout:self.coverImageView clickView:self.coverImageView];

}


Native Ad Click #2

#pragma mark - TnkNativeAdDelegate


- (void) didNativeAdLoad:(TnkNativeAd *)ad {

    NSLog(@"Native Ad Loaded : %@", ad.logicName);

    

    // Rendering native Ad

    [self.coverImageView setImage:[ad getCoverImage]];

    [self.iconImageView setImage:[ad getIconImage]];

    [self.titleLabel setText:[ad getTitle]];

    [self.descLabel setText:[ad getDescription]];

    

    [ad attachLayout:self.coverImageView];

}


// You invokes handleClick method.

- (IBAction) didNativeAdButtonPressed:(id)sender {

    [tnkNative handleClick];

}


[TnkNativeAdDelegate]

To catch events when an native ad is loaded or users click native ads, use TnkNativeAdDelegate protocol.

TnkNativeAdDelegate Protocol

#define AD_FAIL_NO_AD       -1  // no ad available

#define AD_FAIL_NO_IMAGE    -2  // ad image not available

#define AD_FAIL_TIMEOUT     -3  // ad arrived after 5 secs.


#define AD_FAIL_SYSTEM      -9


@protocol TnkNativeAdDelegate <NSObject>


@optional


- (void) didNativeAdFail:(TnkNativeAd *)ad error:(int)errCode;

- (void) didNativeAdLoad:(TnkNativeAd *)ad;

- (void) didNativeAdClick:(TnkNativeAd *)ad;

- (void) didNativeAdShow:(TnkNativeAd *)ad;


@end


[TnkNativeAd APIs]

TnkNativeAd Interface

@interface TnkNativeAd : NSObject <TnkServiceCallback>


@property (nonatomic, weak) id<TnkNativeAdDelegate> delegate;

@property (nonatomic, strong) NSString *logicName;

@property (nonatomic, assign) NSInteger adStyle;


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

- (void) prepare:(NSString *)logicName delegate:(id<TnkNativeAdDelegate>)delegate;

- (void) prepare;


// Common data for PPI, CPC

- (UIImage *) getCoverImage;

- (UIImage *) getIconImage;

- (NSString *) getTitle;

- (NSString *) getDescription;


// for PPI only

- (NSString *) getPointName;

- (NSNumber *) getRewardPoint;

- (NSInteger) getRewardType;

- (NSString *) getActionText;


// attach, detach

- (void) attachLayout:(UIView *)view;

- (void) attachLayout:(UIView *)view clickView:(UIView *)clickView;

- (UIView *) getAttachedLayout;


- (void) detachLayout;


- (void) handleClick;

@end


- properties

 property Description
 logicNameEnter TNK_PPI for incentive ads or TNK_CPC for non-incentive ads. Or any string for Display Logic. See 5) Display Logic for details.

 delegate instance of TnkNativeAdDelegate 
 adStyle Style of Native Ad (See Ad Style)

- Ad Style

We support four styles of Native ads.

- Cover Image
  • AD_STYLE_TEXT_ONLY : Native Ad wihtout cover image.
  • AD_STYLE_PORTRAIT : Native Ad with a cover image in portrait style.
  • AD_STYLE_LANDSCAPE : Native Ad with a cover image in landscape style.
  • AD_STYLE_SQUARE : Native Ad with a cover image in square style. (Not supported yet)
- Ad Icon
  • AD_STYLE_ICON : Native Ad with icon image.
You can do OR operation on Cover Image and Ad Icon value.

Example

Ad Style Example

tnkNative = [[TnkNativeAd alloc] init];

    

// Landscape cover image with icon image

tnkNative.adStyle = AD_STYLE_LANDSCAPE | AD_STYLE_ICON


// Portrait cover image with icon image 

tnkNative.adStyle = AD_STYLE_PORTRAIT;  


// Not cover image but icon image

tnkNative.adStyle = AD_STYLE_ICON;  


// Not cover image and icon image but text only

tnkNative.adStyle = AD_STYLE_TEXT_ONLY;  


- prepare

Method
  - (void) prepare
  - (void) prepare:(NSString *)logicName
  - (void) prepare:(NSString *)logicName delegate:(id<TnkNativeAdDelegate>)delegate

Description
Loading a native ad from server. You can specify a name of Display Logic.
  • When a native ad is loaded, didNativeAdLoad: is invoked. 
  • On failure of loading a native ad, didNativeAdFail:error: is invoked. 
- Native Ad Properties

TnkNativeAd instance provides following methods to access the data of native Ads.
  • - (NSString *) getTitle : Ttile of Ad. 
  • - (NSString *) getDescription : Subtitle of Ad.
  • - (UIImage *) getIconImage : Icon image. Nil returned if you didn't specify AD_STYLE_ICON.
  • - (UIImage *) getCoverImage : Cover image. nil is returend if you didn't specify AD_STYLE_PORTRAIT nor AD_STYLE_LANDSCAPE.
Following methods are for incentive Ads.
  • - (NSNumber *) getRewardPoint : Amount of reward point to user.
  • - (NSString *) getPointName : Name of point (ex :Coin, Ruby, Gold) 
  • - (NSInteger) getRewardType : Type of user action to get reward.(1 for app execution, 2 for specific action)
  • - (NSString *) getActionText :Description of how to get reward.

- Attach & Detach Layout

You should call attachLayout: method on the instance of TnkNativeAd when the native Ad is displayed.

- (void) attachLayout:(UIView *)view
   Attach the TnkNativeAd with a view. Clicks on the view will be handled by SDK.

- (void) attachLayout:(UIView *)view clickView:(UIView *)clickView;
   Attach the TnkNativeAd with a view. Clicks on clickView will be handled by SDK.

- (void) detachLayout
When you remove the view of native ad or reload the native ad, you should invoke detachLayout: on the native Ad. Once detachLayout: method is invoked, the instance of TnkNativeAd can be reused to load another Native Ad.

Lifecycle of NativeAdItem is shown below.

 Instantiated -> prepare -> attachLayout -> detachLayout
                               ^                                                  |
                               +---------------------------+
Comments