Skip to content
This repository was archived by the owner on Apr 5, 2024. It is now read-only.

Implementation for native ads

fan-w-suzuki edited this page Jul 8, 2020 · 16 revisions

Preparation

Please refer to the link below if you have not downloaded the SDK or created an ad space.

Integrating the SDK

If you have not added the SDK into the Project, please add it by the following methods.

Implementation Procedure for Native Ads

There are 3 steps to implement Native Ads.

  1. Assign View with the ad information Layout
  2. Load Native Ads
  3. Showing the loaded ad information in View

Please prepare View in order to display ads from the app side.
For ad display View - UIView and its subclass must be based on the NADNativeViewRendering protocol.

For NADNativeViewRenderingProtocol, we will provide IF so the app can notify the SDK with the following elements below.

  • Ad Explicitly
  • Ad content
  • Ad sentence
  • Promotion name
  • Promotion URL
  • Action button
  • Ad image
  • Logo image

Details of each IF is as follows:

Message Implementation Details
- (UILabel *)prTextLabel required Please return UILabel to display ad explicitly.
- (UILabel *)shortTextLabel optional Please return UILabel to display ad content.
- (UILabel *)longTextLabel optional Please return UILabel to display ad sentence.
- (UILabel *)promotionNameLabel optional Please return UILabel to display promotion name.
- (UILabel *)promotionUrlLabel optional Please return UILabel to display the URL.
- (UILabel *)actionButtonTextLabel optional Please return UILabel to display action button text.
- (UIImageView *)adImageView optional Please return UILabel to display ad image.
- (UIImageView *)logoImageView optional Please return UILabel to display logo image.

For View that is returned through NADNativeViewRendering, it is required to be the descendent of View that conforms to the NADNativeViewRendering Protocol

Please display the native ads withing the following limits:

  • It is required to display the ad explicitly
  • Either Ad content, sentence or promotion needs to be displayed
  • Ad image must be displayed if you select this option in the ad space
  • The image needs to be displayed by the following limits:
Element Scale Clip
Ad Image 30%~150% Up and down, left and right, below 6% each
Logo Image No limits prohibited
Please make sure the ratio is the same when scaling.

Sample of an ad display View that is defined by XIB

image

Swift
import NendAd

class YourNativeAdView: UIView, NADNativeViewRendering {

    @IBOutlet private weak var nativeAdPrTextLabel: UILabel!
    @IBOutlet private weak var nativeAdShortTextLabel: UILabel!
    @IBOutlet private weak var nativeAdLongTextLabel: UILabel!
    @IBOutlet private weak var nativeAdPromotionNameLabel: UILabel!
    @IBOutlet private weak var nativeAdPromotionUrlLabel: UILabel!
    @IBOutlet private weak var nativeAdActionButtonTextLabel: UILabel!
    @IBOutlet private weak var nativeAdImageView: UIImageView!
    @IBOutlet private weak var nativeAdLogoImageView: UIImageView!

    // MARK: - NADNativeViewRendering

    func prTextLabel() -> UILabel! {
        return self.nativeAdPrTextLabel
    }

    func shortTextLabel() -> UILabel! {
        return self.nativeAdShortTextLabel
    }

    func longTextLabel() -> UILabel! {
        return self.nativeAdLongTextLabel
    }

    func promotionNameLabel() -> UILabel! {
        return self.nativeAdPromotionNameLabel
    }

    func promotionUrlLabel() -> UILabel! {
        return self.nativeAdPromotionUrlLabel
    }

    func actionButtonTextLabel() -> UILabel! {
        return self.nativeAdActionButtonTextLabel
    }

    func adImageView() -> UIImageView! {
        return self.nativeAdImageView
    }

    func logoImageView() -> UIImageView! {
        return self.nativeAdLogoImageView
    }

}
Objective-C
#import <NendAd/NADNativeViewRendering.h>

@interface YourNativeAdView () <NADNativeViewRendering>

@property (nonatomic, weak) IBOutlet UILabel *nativeAdPrTextLabel;
@property (nonatomic, weak) IBOutlet UILabel *nativeAdShortTextLabel;
@property (nonatomic, weak) IBOutlet UILabel *nativeAdLongTextLabel;
@property (nonatomic, weak) IBOutlet UILabel *nativeAdPromotionNameLabel;
@property (nonatomic, weak) IBOutlet UILabel *nativeAdPromotionUrlLabel;
@property (nonatomic, weak) IBOutlet UILabel *nativeAdActionButtonTextLabel;
@property (nonatomic, weak) IBOutlet UIImageView *nativeAdImageView;
@property (nonatomic, weak) IBOutlet UIImageView *nativeAdLogoImageView;

@end

@implementation YourNativeAdView

...

#pragma mark - NADNativeViewRendering

- (UILabel *)prTextLabel
{
    return self.nativeAdPrTextLabel;
}

- (UILabel *)shortTextLabel
{
    return self.nativeAdShortTextLabel;
}

- (UILabel *)longTextLabel
{
    return self.nativeAdLongTextLabel;
}

- (UILabel *)promotionNameLabel
{
    return self.nativeAdPromotionNameLabel;
}

- (UILabel *)promotionUrlLabel
{
    return self.nativeAdPromotionUrlLabel;
}

- (UILabel *)actionButtonTextLabel
{
    return self.nativeAdActionButtonTextLabel;
}

- (UIImageView *)adImageView
{
    return self.nativeAdImageView;
}

- (UIImageView *)logoImageView
{
    return self.nativeAdLogoImageView;
}

@end

First, generate NADNAtiveClient Class Instance.

Swift
import NendAd

// Maintain Instance
private var client: NADNativeClient!

...

self.client = NADNativeClient(spotID: spotID, apiKey: "apiKey")
Objective-C
#import <NendAd/NADNativeClient.h>

// Maintain Instance
@property (nonatomic, strong) NADNativeClient *client;

...

self.client = [[NADNativeClient alloc] initWithSpotID:spotID apiKey:@"apiKey"];

The information to generate Instance are as follows:

Argument Name Type Explanation
spotID NSInteger SpotID issued from the dashboard
apiKey NSString ApiKey issued from the dashboard

Next, you will load the ad by the loadWithCompletionBlock: method of NADNativeClient Class.
If loading is successful, then the NADNative object will be passed to the argument Blocks.
If the load fails, then the NSError Object will be passed to the argument Blocks with the error information.

Swift
self.client.load() { (ad, error) in
    if let nativeAd = ad {
        // Success
    } else {
        // Failed
        print("error:\(error!)")
    }
}
Objective-C
[self.client loadWithCompletionBlock:^(NADNative *ad, NSError *error) {
    if (ad) {
        // Success
    } else {
        // Failed
        NSLog(@"error: %@", error);
    }
}];

The following are the error codes of NSError when loading fails:

Code Details Note
kNADNativeErrorCodeFailedToRequest Request failed No internet connection, etc. (You can check the localizedDescription in NSError for more information)
kNADNativeErrorCodeInvalidResponseType Invalid Response Type SDK Internal error (rare case)

You can display the ad information within View by the intoView:advertisingExplicitly: method in NADNative Class that was acquired before.
Information is as follows:

Argument Name Type Explanation
adView UIView<NADNativeViewRendering> View of the ad information layout
advertisingExplicitly NADNativeAdvertisingExplicitly Wording of the ad explicitly

The definition of NADNativeAdvertisingExplicitly is as follows:

Name Wording
NADNativeAdvertisingExplicitlyPR PR
NADNativeAdvertisingExplicitlySponsored Sponsored
NADNativeAdvertisingExplicitlyAD 広告
NADNativeAdvertisingExplicitlyPromotion プロモーション
Swift
var nativeAd: NADNative = ... // Ad Object
var adView: YourNativeAdView = ... // View of the ad information layout
nativeAd.intoView(adView, advertisingExplicitly: .PR)
Objective-C
NADNative *nativeAd = ...; // Ad Object
YourNativeAdView *adView = ...; // View of the ad information layout
[nativeAd intoView:adView advertisingExplicitly:NADNativeAdvertisingExplicitlyPR];

You can reload ads by the enableAutoReloadWithInterval:completionBlock method of NADNativeClient Class.

Swift
self.client.enableAutoReload(withInterval: interval, completionBlock: { (ad, error) in
	if let nativeAd = ad {
        // Successful
    } else {
        // Failed
    }
})
Objective-C
[self.client enableAutoReloadWithInterval:interval completionBlock:^(NADNative *ad, NSError *error) {
    if (ad) {
        // Successful
    } else {
        // Failed
        NSLog(@"error: %@", error);
    }
}];

Argument Name Type Explanation
interval NSTimeInterval Auto Reload Interval

Please set the reload interval time to at least 30 seconds.

You can stop auto reload by the disableAutoReload Method in NADNativeClient Class.

Swift
self.client.disableAutoReload()
Objective-C
[self.client disableAutoReload];

You can receive ad event notifications by setting the NADNativeDelegate Protocol of NADNative Class to delegate.

Swift
class YourClass: NSObject, NADNativeDelegate {

    private var client: NADNativeClient!

    ...

    private func exampleLoadAd() {
        self.client.load() { (ad, error) in
            if let nativeAd = ad {
                nativeAd.delegate = self
            }
		}
    }

    // MARK: - NADNativeDelegate

    // It will be called when the ad is clicked
    func nadNativeDidClickAd(_ ad: NADNative!) {
        print("Click ad.")
    }
}
Objective-C
#import <NendAd/NADNativeClient.h>

@interface YourClass : NSObject <NADNativeDelegate>

@property (nonatomic, strong) NADNativeClient *client;

@end

...

import NendAd

@implementation YourClass

...
__weak typeof(self) weakSelf = self;
[self.client loadWithCompletionBlock:^(NADNative *ad, NSError *error) {
    if (ad) {
        ad.delegate = weakSelf;
        ...
 	}
}];
...

@end
...

#pragma mark - NADNativeDelegate

///-----------------------------------------------
/// @optional
///-----------------------------------------------

// It will be called when the ad is clicked
- (void)nadNativeDidClickAd:(NADNative *)ad
{
    NSLog(@"click ad.");
}

Verification

日本語

nendSDK iOS について

SDKの組み込み

広告の表示

全般設定

導入サポート


English

About nendSDK iOS

SDK Implementation

Display Ads

Global Settings

Supports

Clone this wiki locally