iOS Banner - adxcorp/ADXLibrary_Integration GitHub Wiki

Banner Integration

1) Banner Ad ๊ตฌํ˜„

MyViewController.h

#import "MPAdView.h"

@interface MyViewController : UIViewController <MPAdViewDelegate>

@property (nonatomic) MPAdView *adView;

@end

MyViewController.m

#import "MyViewController.h"

@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.adView = [[MPAdView alloc] initWithAdUnitId:@"<YOUR_ADUNIT_ID_HERE>"
                                                     size:MOPUB_BANNER_SIZE];
    self.adView.delegate = self;
    self.adView.frame = CGRectMake((self.view.bounds.size.width - MOPUB_BANNER_SIZE.width) / 2, 
                                  self.view.bounds.size.height - MOPUB_BANNER_SIZE.height,
                                  MOPUB_BANNER_SIZE.width, MOPUB_BANNER_SIZE.height);
    [self.view addSubview:self.adView];
    [self.adView loadAd];
    
}

#pragma mark - <MPAdViewDelegate>
- (UIViewController *)viewControllerForPresentingModalView {
    return self;
}
...
@end

2) Delegate Method

MPAdViewDelegate์—๋Š” ๊ด‘๊ณ ๊ฐ€ ์„ฑ๊ณต์ ์œผ๋กœ ๋กœ๋“œ ๋˜์—ˆ์„ ๋•Œ๋‚˜ modal view๊ฐ€ present ๋˜๊ฑฐ๋‚˜ dismiss ๋  ๋•Œ ๋“ฑ, ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์ด๋ฒคํŠธ callback์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. MPAdview.h์˜ MPAdViewDelegate protocol์„ ํ™•์ธํ•ด๋ณด์‹œ๋ฉด ์ด๋Ÿฌํ•œ callback method์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ํ™•์ธํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•„์š”์— ๋”ฐ๋ผ ์‚ฌ์šฉํ•ด์ฃผ์„ธ์š”.

3) Handling rotation

rotation ์ง€์›์ด ํ•„์š”ํ•˜์‹œ๋‹ค๋ฉด orientation์ด ๋ณ€๊ฒฝ๋  ๋•Œ์— adView์˜ rotateToOrientation: ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด์ฃผ์„ธ์š”. ๋ช‡๋ช‡ ์ผ€์ด์Šค์—์„œ๋Š” (e.g. iAd) ํšŒ์ „ํ• ๋•Œ ๊ด‘๊ณ  ์ปจํ…์ธ ์˜ ์‚ฌ์ด์ฆˆ๊ฐ€ ๋ณ€๊ฒฝ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ผ€์ด์Šค๋ฅผ ์œ„ํ•˜์—ฌ orientation์ด ๋ณ€๊ฒฝ๋œ ํ›„ ์‚ฌ์ด์ฆˆ์™€ ํฌ์ง€์…˜์„ ๋‹ค์‹œ ์žก์•„์ฃผ์„ธ์š”.

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
                                duration:(NSTimeInterval)duration {
    [self.adView rotateToOrientation:toInterfaceOrientation];
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    CGSize size = [self.adView adContentViewSize];
    CGFloat centeredX = (self.view.bounds.size.width - size.width) / 2;
    CGFloat bottomAlignedY = self.view.bounds.size.height - size.height;
    self.adView.frame = CGRectMake(centeredX, bottomAlignedY, size.width, size.height);
}