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

Implementation for fullscreen ads

fan-w-suzuki edited this page Jul 8, 2020 · 12 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

First, load the ad using NADFullBoardLoader.

Swift
import UIKit
import NendAd

class ViewController: UIViewController {

    private var adLoader: NADFullBoardLoader!
    private var ad: NADFullBoard? // An instance of a fullBoard Ad

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        self.adLoader = NADFullBoardLoader(spotID: spotID, apiKey: "apiKey")
    }

    private func loadFullBoardAd() {
        self.adLoader.loadAd { [weak self] (ad: NADFullBoard?, error: NADFullBoardLoaderError) in
            guard let `self` = self else { return }
            if let fullBoardAd = ad {
                self.ad = fullBoardAd
            } else {
                switch (error) {
                case .failedAdRequest:
                    print("The ad request failed.")
                    break
                case .failedDownloadImage:
                    print("Failed to download ad images.")
                    break
                case .invalidAdSpaces:
                    print("An inventory that is not available for fullBoard ads has been specified.")
                    break
                default:
                    break
                }
            }
        }
    }
}
Objective-C
#import <NendAd/NADFullBoardLoader.h>

@interface ViewController : UIViewController

@property (nonatomic, strong) NADFullBoardLoader *loader;
@property (nonatomic, strong) NADFullBoard *ad;

@end

...

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    ...

    self.loader = [[NADFullBoardLoader alloc] initWithSpotID:spotID apiKey:@"apiKey"];
}

- (void)loadFullBoardAd
{
    __weak typeof(self) weakSelf = self;
    [self.loader loadAdWithCompletionHandler:^(NADFullBoard *ad, NADFullBoardLoaderError error) {
        if (ad) {
            weakSelf.ad = ad;
        } else {
            switch (error) {
                case NADFullBoardLoaderErrorFailedAdRequest:
                    NSLog(@"The ad request failed.");
                    break;
                case NADFullBoardLoaderErrorFailedDownloadImage:
                    NSLog(@"Failed to download ad images.");
                    break;
                case NADFullBoardLoaderErrorInvalidAdSpaces:
                    NSLog(@"An inventory that is not available for fullBoard ads has been specified.");
                    break;
                default:
                    break;
            }
        }
    }];
}

@end

Next, display the loaded ad in any way.

  • When modal display is performed on the full screen

    Swift
    class ViewController: UIViewController {
    
        private func showFullBoardAd() {
            if let ad = self.ad {
                // The `UIViewController` passed as the first argument must be properly placed on the` UIWindow` hierarchy
                ad.show(from: self)
            }
        }
    
    }
    Objective-C
    @implementation ViewController
    
    - (void)showFullBoardAd
    {
        [self.ad showFromViewController:self];
    }
    
    @end
  • Displaying the advertisement screen in an arbitrary way

    Swift
    class ViewController: UIViewController {
    
        private func showCustomFullBoardAd() {
            if let ad = self.ad {
                // You can get `UIViewController` for ad display from the` NADFullBoard` object
                let adViewController = ad.fullBoardAdViewController()
    
                // The acquired 'UIViewController` can be displayed by the application in an arbitrary way
                // If you retrieve `view` of` UIViewController` and display it with `addSubView:` in any `UIView`, keep the instance on the application side so that the acquired` UIViewController` will not be released
    
                // e.g. push
                self.navigationController!.pushViewController(adViewController!, animated: true)
    
                // In addition, you can also display `close button` which SDK side prepared by the following method
                adViewController?.enableCloseButton(clickHandler: {
                    // Implement processing when the close button is clicked
                })
            }
        }
    
    }
    Objective-C
    @implementation ViewController
    
    - (void)showCustomFullBoardAd
    {
        UIViewController<NADFullBoardView> *adViewController = [self.ad fullBoardAdViewController];
        [self.navigationController pushViewController:adViewController animated:YES];
    
        [adViewController enableCloseButtonWithClickHandler:^{
            ...
        }];
    }
    
    @end

Note: When you close the fullscreen ads, the ad is not automatically reloaded. When updating the advertisement contents, you need to execute the loading process again.

Event notification

You can get various advertisement events in one of the following ways.

  • When modal display is performed on the full screen

    Swift
    // Implement `NADFullBoardDelegate`
    class ViewController: UIViewController, NADFullBoardDelegate {
    
        private func loadFullBoardAd() {
            self.adLoader.loadAd { [weak self] (ad: NADFullBoard?, error: NADFullBoardLoaderError) in
                guard let `self` = self else { return }
                if let fullBoardAd = ad {
                    fullBoardAd.delegate = self
                    ...
                }
            }
        }
    
        // MARK: - NADFullBoardDelegate
    
        // Called when advertisement is displayed
        func nadFullBoardDidShowAd(_ ad: NADFullBoard!) {
            print(#function)
        }
    
        // Called when an ad is clicked
        func nadFullBoardDidClickAd(_ ad: NADFullBoard!) {
            print(#function)
        }
    
        // Called when closing ad
        func nadFullBoardDidDismissAd(_ ad: NADFullBoard!) {
            print(#function)
        }
    
    }
    Objective-C
    @interface ViewController () <NADFullBoardDelegate>
    ...
    @end
    
    @implementation ViewController
    
    - (void)loadFullBoardAd
    {
      __weak typeof(self) weakSelf = self;
      [self.loader loadAdWithCompletionHandler:^(NADFullBoard *ad, NADFullBoardLoaderError error) {
          if (ad) {
              ad.delegate = weakSelf;
              ...
          }
      }];
    }
    
    #pragma mark - NADFullBoardDelegate
    
    - (void)NADFullBoardDidShowAd:(NADFullBoard *)ad
    {
        NSLog(@"%s", __FUNCTION__);
    }
    
    - (void)NADFullBoardDidClickAd:(NADFullBoard *)ad
    {
        NSLog(@"%s", __FUNCTION__);
    }
    
    - (void)NADFullBoardDidDismissAd:(NADFullBoard *)ad
    {
        NSLog(@"%s", __FUNCTION__);
    }
    @end
  • When displaying the advertisement screen in an arbitrary method

    Swift
    // Implement `NADFullBoardViewDelegate`
    class ViewController: UIViewController, NADFullBoardViewDelegate {
    
        private var ad: NADFullBoard?
    
        private func showCustomFullBoardAd() {
            if let ad = self.ad {
                let adViewController = ad.fullBoardAdViewController()
                adViewController?.delegate = self
    
                ...
            }
        }
    
        // MARK: - NADFullBoardViewDelegate
    
        // Called when an ad is clicked
        func nadFullBoardDidClickAd(_ ad: NADFullBoard!) {
            print(#function)
        }
    
    }
    Objective-C
    @interface ViewController () <NADFullBoardViewDelegate>
    @property (nonatomic, strong) NADFullBoard *ad;
    @end
    
    @implementation ViewController
    
    - (void)showCustomFullBoardAd
    {
        UIViewController<NADFullBoardView> *adViewController = [self.ad fullBoardAdViewController];
        adViewController.delegate = self;
    
        ...
    }
    
    #pragma mark - NADFullBoardViewDelegate
    
    - (void)NADFullBoardDidClickAd:(NADFullBoard *)ad
    {
        NSLog(@"%s", __FUNCTION__);
    }
    
    @end

About background color outside Safe Area

You can change the background color outside iPhoneX Safe Area more affinitive to your app looking & feeling.

  • Sample with background color set to white

image

Swift
class ViewController: UIViewController {

    private var adLoader: NADFullBoardLoader!
    private var ad: NADFullBoard? // An instance of a fullBoard Ad

...

    private func loadFullBoardAd() {
        self.adLoader.loadAd { [weak self] (ad: NADFullBoard?, error: NADFullBoardLoaderError) in
            guard let `self` = self else { return }
            if let fullBoardAd = ad {
                self.ad = fullBoardAd
                self.ad!.backgroundColor = UIColor.white    // Set white as background color
            }
        }
    }
}
Objective-C
@interface ViewController : UIViewController

@property (nonatomic, strong) NADFullBoardLoader *loader;
@property (nonatomic, strong) NADFullBoard *ad;

@end

...

@implementation ViewController

...

- (void)loadFullBoardAd
{
    __weak typeof(self) weakSelf = self;
    [self.loader loadAdWithCompletionHandler:^(NADFullBoard *ad, NADFullBoardLoaderError error) {
        if (ad) {
            weakSelf.ad = ad;
            weakSelf.ad.backgroundColor = [UIColor whiteColor];
        }
    }];
}

@end

By default the background color is black.

image

日本語

nendSDK iOS について

SDKの組み込み

広告の表示

全般設定

導入サポート


English

About nendSDK iOS

SDK Implementation

Display Ads

Global Settings

Supports

Clone this wiki locally