Skip to content

Conversation

@AwakenDragon
Copy link
Contributor

@AwakenDragon AwakenDragon commented Nov 11, 2021

Banner(V2) Integration

1. Init Yodo1MasBannerAdView

For Objective-C

Yodo1MasBannerAdView *bannerAdView = [[Yodo1MasBannerAdView alloc] init];
[bannerAdView setAdSize:Yodo1MasBannerAdSizeBanner];
// TODO: Add bannerAdView to your view hierarchy.

For Swift

let bannerAdView = Yodo1MasBannerAdView()
bannerAdView.setAdSize(.banner)
// TODO: Add bannerAdView to your view hierarchy.

Banner sizes

Size in dp Description Availability AdSize constant
320x50 Banner Phones and Tablets Banner
320x100 Large Banner Phones and Tablets LargeBanner
300x250 IAB Medium Rectangle Phones and Tablets IABMediumRectangle
Full screen width x Adaptive height Adaptive banner Phones and Tablets AdaptiveBanner
Screen width x 32/50/90 Smart banner Phones and Tablets SmartBanner

2. Load an ad

Once the Yodo1MasBannerAdView is in place, the next step is to load an ad. That's done with the loadAd() method in the Yodo1MasBannerAdView class.

Here's an example that shows how to load an ad in the viewDidLoad method of an UIViewController:

For Objective-C

#import <UIKit/UIKit.h>
#import "Yodo1Mas.h"
#import "Yodo1MasBannerAdView.h"
  
@interface MainController ()
  
@property (nonatomic, strong) Yodo1MasBannerAdView *bannerAdView;

@end

@implementation BannerController
  
- (void)viewDidLoad {
  [super viewDidLoad];
  [[Yodo1Mas sharedInstance] initWithAppKey:@"YourAppKey" successful:^{

  } fail:^(NSError * _Nonnull error) {

  }];
  
  _bannerAdView = [[Yodo1MasBannerAdView alloc] init];
  [_bannerAdView setAdSize:Yodo1MasBannerAdSizeBanner];
  [_bannerAdView loadAd];

   CGSize superviewSize = self.view.frame.size;
    CGSize size = _bannerAdView.intrinsicContentSize;
    
    CGPoint origin = CGPointZero;
    origin = CGPointMake((superviewSize.width - size.width) / 2, (superviewSize.height - size.height) / 2); // Yodo1MasAdBannerAlignHorizontalCenter | Yodo1MasAdBannerAlignVerticalCenter
//    origin = CGPointMake((superviewSize.width - size.width) / 2, 0); // Yodo1MasAdBannerAlignHorizontalCenter | Yodo1MasAdBannerAlignTop
//    origin = CGPointMake((superviewSize.width - size.width) / 2, superviewSize.height - size.height); // Yodo1MasAdBannerAlignHorizontalCenter | Yodo1MasAdBannerAlignBottom
//    origin = CGPointMake(0, (superviewSize.height - size.height) / 2); // Yodo1MasAdBannerAlignLeft | Yodo1MasAdBannerAlignVerticalCenter
//    origin = CGPointMake(superviewSize.width - size.width, (superviewSize.height - size.height) / 2); // Yodo1MasAdBannerAlignRight | Yodo1MasAdBannerAlignVerticalCenter
    
    // set offset
    CGPoint offset = CGPointMake(0, 0);
    origin.x += offset.x;
    origin.y += offset.y;
    
    _bannerAdView.frame = CGRectMake(origin.x, origin.y, size.width, size.height);
  [self.view addSubview:_bannerAdView];
}
@end

For Swift

import UIKit
import Yodo1MasCore

class MainController : UIViewController {

	var bannerAdView: Yodo1MasBannerAdView!

	override func viewDidLoad() {
		super.viewDidLoad()
    Yodo1Mas.sharedInstance().initWithAppKey("YourAppKey") {
            
    } fail: { error in

    }
    bannerAdView = Yodo1MasBannerAdView()
    bannerAdView.setAdSize(.banner)
    bannerAdView.loadAd()

    let superviewSize = self.view.frame.size
        let size = bannerAdView.intrinsicContentSize
        
        var origin = CGPoint.zero;
        origin = CGPoint(x: (superviewSize.width - size.width) / 2, y: (superviewSize.height - size.height) / 2) // Yodo1MasAdBannerAlignHorizontalCenter | Yodo1MasAdBannerAlignVerticalCenter
//        origin = CGPoint(x: (superviewSize.width - size.width) / 2, y: 0); // Yodo1MasAdBannerAlignHorizontalCenter | Yodo1MasAdBannerAlignTop
//        origin = CGPoint(x: (superviewSize.width - size.width) / 2, y: superviewSize.height - size.height); // Yodo1MasAdBannerAlignHorizontalCenter | Yodo1MasAdBannerAlignBottom
//        origin = CGPoint(x: 0, y: (superviewSize.height - size.height) / 2); // Yodo1MasAdBannerAlignLeft | Yodo1MasAdBannerAlignVerticalCenter
//        origin = CGPoint(x: superviewSize.width - size.width, y: (superviewSize.height - size.height) / 2); // Yodo1MasAdBannerAlignRight | Yodo1MasAdBannerAlignVerticalCenter
        
        // set offset
        let offset = CGPoint(x: 0, y: 0);
        origin.x += offset.x;
        origin.y += offset.y;
        
        bannerAdView.frame = CGRect(x: origin.x, y: origin.y, width: size.width, height: size.height)
    self.view.addSubview(bannerAdView)
   }
}

That's it! Your app is now ready to display banner ads.

3. Ad events

To further customize the behavior of your ad, you can hook onto a number of events in the ad's lifecycle: loading, opening, closing, and so on. You can listen for these events through the Yodo1MasBannerAdViewDelegate class.

For Objective-C

#import <UIKit/UIKit.h>
#import "Yodo1Mas.h"
#import "Yodo1MasBannerAdView.h"
  
@interface MainController ()<Yodo1MasBannerAdViewDelegate>
  
@property (nonatomic, strong) Yodo1MasBannerAdView *bannerAdView;

@end

@implementation BannerController
  
- (void)viewDidLoad {
  [super viewDidLoad];
  [[Yodo1Mas sharedInstance] initWithAppKey:@"YourAppKey" successful:^{

  } fail:^(NSError * _Nonnull error) {

  }];
  
  _bannerAdView = [[Yodo1MasBannerAdView alloc] init];
  _bannerAdView.adDelegate = self;
  [_bannerAdView setAdSize:Yodo1MasBannerAdSizeBanner];
  [_bannerAdView loadAd];

   CGSize superviewSize = self.view.frame.size;
    CGSize size = _bannerAdView.intrinsicContentSize;
    
    CGPoint origin = CGPointZero;
    origin = CGPointMake((superviewSize.width - size.width) / 2, (superviewSize.height - size.height) / 2); // Yodo1MasAdBannerAlignHorizontalCenter | Yodo1MasAdBannerAlignVerticalCenter
//    origin = CGPointMake((superviewSize.width - size.width) / 2, 0); // Yodo1MasAdBannerAlignHorizontalCenter | Yodo1MasAdBannerAlignTop
//    origin = CGPointMake((superviewSize.width - size.width) / 2, superviewSize.height - size.height); // Yodo1MasAdBannerAlignHorizontalCenter | Yodo1MasAdBannerAlignBottom
//    origin = CGPointMake(0, (superviewSize.height - size.height) / 2); // Yodo1MasAdBannerAlignLeft | Yodo1MasAdBannerAlignVerticalCenter
//    origin = CGPointMake(superviewSize.width - size.width, (superviewSize.height - size.height) / 2); // Yodo1MasAdBannerAlignRight | Yodo1MasAdBannerAlignVerticalCenter
    
    // set offset
    CGPoint offset = CGPointMake(0, 0);
    origin.x += offset.x;
    origin.y += offset.y;
    
    _bannerAdView.frame = CGRectMake(origin.x, origin.y, size.width, size.height);
  [self.view addSubview:_bannerAdView];
}


#pragma mark - Yodo1MasBannerAdViewDelegate
- (void)onBannerAdLoaded:(Yodo1MasBannerAdView *)banner {
    
}

- (void)onBannerAdFailedToLoad:(Yodo1MasBannerAdView *)banner withError:(Yodo1MasError *)error {
    
}

- (void)onBannerAdOpened:(Yodo1MasBannerAdView *)banner {

}

- (void)onBannerAdFailedToOpen:(Yodo1MasBannerAdView *)banner withError:(Yodo1MasError *)error {
    
}

- (void)onBannerAdClosed:(Yodo1MasBannerAdView *)banner {

}

@end

For Swift

import UIKit
import Yodo1MasCore

class MainController: UIViewController {
    var bannerAdView: Yodo1MasBannerAdView!

    override func viewDidLoad() {
        super.viewDidLoad()
        
        Yodo1Mas.sharedInstance().initWithAppKey("YourAppKey") {
            
        } fail: { error in
            
        }
        
        bannerAdView = Yodo1MasBannerAdView()
        bannerAdView.setAdSize(.banner)
        bannerAdView.adDelegate = self
        bannerAdView.loadAd()

        let superviewSize = self.view.frame.size
        let size = bannerAdView.intrinsicContentSize
        
        var origin = CGPoint.zero;
        origin = CGPoint(x: (superviewSize.width - size.width) / 2, y: (superviewSize.height - size.height) / 2) // Yodo1MasAdBannerAlignHorizontalCenter | Yodo1MasAdBannerAlignVerticalCenter
//        origin = CGPoint(x: (superviewSize.width - size.width) / 2, y: 0); // Yodo1MasAdBannerAlignHorizontalCenter | Yodo1MasAdBannerAlignTop
//        origin = CGPoint(x: (superviewSize.width - size.width) / 2, y: superviewSize.height - size.height); // Yodo1MasAdBannerAlignHorizontalCenter | Yodo1MasAdBannerAlignBottom
//        origin = CGPoint(x: 0, y: (superviewSize.height - size.height) / 2); // Yodo1MasAdBannerAlignLeft | Yodo1MasAdBannerAlignVerticalCenter
//        origin = CGPoint(x: superviewSize.width - size.width, y: (superviewSize.height - size.height) / 2); // Yodo1MasAdBannerAlignRight | Yodo1MasAdBannerAlignVerticalCenter
        
        // set offset
        let offset = CGPoint(x: 0, y: 0);
        origin.x += offset.x;
        origin.y += offset.y;
        
        bannerAdView.frame = CGRect(x: origin.x, y: origin.y, width: size.width, height: size.height)
        self.view.addSubview(bannerAdView)
    }
}

extension MainController: Yodo1MasBannerAdViewDelegate {
    // MARK: Yodo1MasBannerAdViewDelegate
    func onBannerAdLoaded(_ banner: Yodo1MasBannerAdView) {
        
    }
    
    func onBannerAdFailed(toLoad banner: Yodo1MasBannerAdView, withError error: Yodo1MasError) {
        
    }
    
    func onBannerAdOpened(_ banner: Yodo1MasBannerAdView) {
        
    }
    
    func onBannerAdFailed(toOpen banner: Yodo1MasBannerAdView, withError error: Yodo1MasError) {
        
    }
    
    func onBannerAdClosed(_ banner: Yodo1MasBannerAdView) {
        
    }
    
}

@Sunmeng1985 Sunmeng1985 merged commit ead9e24 into 4.4.0 Nov 12, 2021
@Sunmeng1985 Sunmeng1985 deleted the 4.4.0-iOS branch November 15, 2021 02:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants