Follow

iOS SDK Native Integration

 

AerServ’s iOS SDK, will offer two forms of integration for design and layout of the native ad. An Interface Builder implementation that utilizes a NIB file to link UI elements to native assets and a custom implementation that will return raw values of string text or string URLs for the native assets. Additionally, we offer a UITableView subclass that can be utilized to automatically display native ads at certain indices and/or at a varying frequency.

 

Table of Contents

 

 

1. Building Native Ad

 

All native ads will be created as a subclass of the ASNativeAdView class, which is a subclass of a UIView class.

 

@interface NativeTest1 : ASNativeAdView

 

There are three protocols that are associated with the ASNativeAdView class, the ASNativeAdDelegate, ASNativeAdViewElements, and ASNativeAdCustomDelegate. The ASNativeAdDelegate protocol will handle all callback events associated with the loading of the ad such as did load, did preload, did fail, did click, and did load/show with transaction info.

Laying out the actual design can be done with either the ASNativeAdViewElements or the ASNativeAdCustomDelegate protocols.

 

1.1 - Interface Builder Implementation 

 

Conforming the ASNativeAdView subclass to the ASNativeAdViewElements protocol would link the subclass to a NIB file created through Interface Builder.

IBOutlet properties would be created in the subclass for the assets in your design.

 

@interface NativeTest1 () 

@property (nonatomic, weak) IBOutlet UIImageView* mainImgView;
@property (nonatomic, weak) IBOutlet UILabel* titleLbl;
@property (nonatomic, weak) IBOutlet UILabel* sponsoredLbl;
@property (nonatomic, weak) IBOutlet UILabel* primaryDescLbl;

@end

 

Overload the –initWithCoder: initializer for the subclass and assign the ASNativeAdViewElements delegate to the subclass.

 

- (instancetype)initWithCoder:(NSCoder*)aDecoder {
    if(self = [super initWithCoder:aDecoder]) {
        self.viewDelegate = self;
    }
    return self;
}

 

Link the IBOutlet properties to the assets in the ASNativeAdViewElements call back methods.

 

#pragma mark - ASNativeAdViewElements Protocol Methods

- (UIImageView*)nativeMainImgViewFromAdView:(ASNativeAdView *)nativeView { return self.mainImgView; }
- (UILabel*)nativeSponsoredLblFromAdView:(ASNativeAdView*)nativeView { return self.sponsoredLbl; }
- (UILabel*)nativePrimaryDescLblFromAdView:(ASNativeAdView*)nativeView { return self.primaryDescLbl; }
- (UILabel*)nativeMainTitleLblFromAdView:(ASNativeAdView *)nativeView { return self.titleLbl; }

 

Lastly, set up your native ad design and layout through the NIB file. Define the class of your NIB file to the subclass you created.

 

Screen_Shot_2017-08-23_at_2.33.58_PM.png 

 

1.2 - Custom Implementation 

 

If you would rather deal with the raw assets as strings or string URLs and build the native ad through other means. Conform a class to the ASNativeAdCustomDelegate, and assign the ASNativeAdCustomDelegate to that class. For the example below, we just used the subclass to conform the protocol to and assigned the delegate to itself in the initializer.

 

@interface NativeTestCustom : ASNativeAdView 

@end
@implementation NativeTestCustom
- (instancetype)init { if(self = [super init]) { self.customDelegate = self; } return self; }

 

In the class that conforms to the ASNativeAdCustomDelegate protocol, define the callback methods for the assets desired.

 

#pragma mark - ASNativeAdCustomDelegate Protocol Methods

- (void)nativeAdView:(ASNativeAdView*)nativeView hasTitle:(NSString*)titleStr {
    NSLog(@"-- title: %@", titleStr);
}

- (void)nativeAdView:(ASNativeAdView*)nativeView hasSponsored:(NSString*)sponsoredStr { NSLog(@"-- sponsored: %@", sponsoredStr); }
- (void)nativeAdView:(ASNativeAdView*)nativeView hasPrimaryDesc:(NSString*)primaryDescStr { NSLog(@"-- primaryDesc: %@", primaryDescStr); }
- (void)nativeAdView:(ASNativeAdView*)nativeView hasMainImgUrl:(NSString*)mainImgUrlStr withSize:(CGSize)imgSize { NSLog(@"-- mainImgUrl: %@, imgSize: %@", mainImgUrlStr, NSStringFromCGSize(imgSize)); }
- (void)nativeAdView:(ASNativeAdView*)nativeView hasVastTag:(NSString*)vastTagStr { NSLog(@"-- vastTagStr: %@", vastTagStr); }

 

Additional methods are implemented for ASNativeAdView to return the click through URL and to fire click tracking. Both methods take in an ENUM parameter that will link to a view element.

 

typedef NS_ENUM(NSInteger, ASNativeElementType) {
 kNativeElementTypeAdView = 0,
 kNativeElementTypeTitle,
 kNativeElementTypeSponsored,
 kNativeElementTypePrimaryDescription,
 kNativeElementTypeMainImage,
 kNativeElementTypeVideo,
 kNativeElementTypeIconImage,
 kNativeElementTypeRating,
 kNativeElementTypeLikes,
 kNativeElementTypeDownloads,
 kNativeElementTypePrice,
 kNativeElementTypeSalePrice,
 kNativeElementTypePhoneNumber,
 kNativeElementTypeStreetAddress,
 kNativeElementTypeSecondaryDescription,
 kNativeElementTypeWebsiteUrl,
 kNativeElementTypeCallToAction
};

- (void)fireClickEventForElement:(ASNativeElementType)type;
- (NSURL*)linkForElement:(ASNativeElementType)type;

 

 

2. Loading Native Ads

 

There are two different implementations for loading the native ad that was built in the previous section. You can load the ad individually or through a UITableView subclass.

 

2.1 - Individual Native Ads 

 

For the Interface Builder implementation, create the ad by loading the NIB file name through the bundle and taking the first object.

 

@property (nonatomic, strong) NativeTest1* nativeAdOne;
…
self.nativeAdOne = [[[NSBundle mainBundle] loadNibNamed:@"NativeTest1"
                                                  owner:self.nativeAdOne
                                                options:nil] firstObject];

 

For the custom implementation, initialization as a new object is all that’s necessary.

 

@property (nonatomic, strong) NativeTestCustom* nativeAdCustom;
…
self.nativeAdCustom = [NativeTestCustom new];

 

After instantiating the native ad objects, call the method –configurePlacement:andDelegate to setup the native ad’s placement and native ad delegate. You can optionally request preload with the isPreload flag, or define keywords or pubKeys. When ready call –loadAd.

 

[self.nativeAdOne configurePlacement:@"NATIVE_PLACEMENT_ID" andDelegate:self];
self.nativeAdOne.isPreload = YES;
self.nativeAdOne.keywords = @[@"native", "test"];
[self.nativeAdOne loadAd];

 

 

 

Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request

Comments