Blog

SpriteKit에 iAd, AdMob 붙이기

February 21, 2014

SpriteKit에 iAd, AdMob 붙이기

* iAd는 UIViewController에만 붙는다. 

iAd 문서

https://developer.apple.com/library/ios/documentation/userexperience/Reference/iAd_ReferenceCollection/_index.html

Google AdMob 문서

https://developers.google.com/mobile-ads-sdk/docs/admob/fundamentals#ios

두 개의 문서를 참조하여 필요한 프레임 워크 및 설정을 하도록 하자. AdMob에서는 Other Linker Flags에서 -ObjC 설정을 안해주면 뭔가 에러를 나니 꼭 문서를 참조해서 설정하도록 하자. 


나머지는 대충 코드를 보면 알수 있다. 설명 생략. 


UIViewController.h

#import <UIKit/UIKit.h>

#import <SpriteKit/SpriteKit.h>

#import “GADBannerView.h”

#import <iAd/iAd.h>

@interface ViewController : UIViewController <ADBannerViewDelegate, GADBannerViewDelegate> {

  ADBannerView* iADBanner; // iAD

  GADBannerView* adMobBanner; // 애드몹

}

– (void)initADBanner;

– (void)hideADBanner;

UIViewController.m

// 광고 생성 초기.

– (void)initADBanner {

  // iAd Banner

  iADBanner = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];

  iADBanner.frame = CGRectZero;

  iADBanner.delegate=self;

  [self.view addSubview:iADBanner];

  

  // AdMob Banner

  adMobBanner = [[GADBannerView alloc] initWithFrame:CGRectMake(0, 578, 320, 50)];

  adMobBanner.rootViewController = self;

  adMobBanner.delegate = self;

  adMobBanner.adUnitID = @”google AdMob 광고 id”;

  [self.view addSubview:adMobBanner];

  

  // 애드몹 광고 request

  GADRequest* requestAd = [GADRequest request];

  

  // test. 테스트 주석

//  [requestAd setTestDevices: [NSArray arrayWithObjects:GAD_SIMULATOR_ID, nil]];

  [adMobBanner loadRequest:requestAd];

}

– (void)hideADBanner {

  for (UIView *subview in [self.view subviews]) {

    if([subview isKindOfClass:[GADBannerView class]]) {

      [subview removeFromSuperview];

    }

    if([subview isKindOfClass:[ADBannerView class]]) {

      [subview removeFromSuperview];

    }

  }}

#pragma mark –

#pragma mark iAD Delegate

– (void)bannerViewDidLoadAd:(ADBannerView *)banner {

  [UIView beginAnimations:@”animateBannerAppear” context:nil];

  [iADBanner setFrame:CGRectMake(0, 0, 320, 50)];

  [UIView commitAnimations];

}

– (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {

  [UIView beginAnimations:@”animateBannerOff” context:nil];

  [iADBanner setFrame:CGRectMake(0, 0, 320, 50)];

  [UIView commitAnimations];

}

#pragma mark –

#pragma mark adMob Delegate

– (void)adViewDidReceiveAd:(GADBannerView *)bannerView {

  [UIView beginAnimations:@”BannerSlide” context:nil];

  [adMobBanner setFrame:CGRectMake(0, 518, 320, 50)];

  [UIView commitAnimations];

}

– (void)adView:(GADBannerView *)bannerView didFailToReceiveAdWithError:(GADRequestError *)error {

  [UIView beginAnimations:@”BannerSlide” context:nil];

  [adMobBanner setFrame:CGRectMake(0, 518, 320, 50)];

  [UIView commitAnimations];

}

광고를 호출하는 곳.m

#import “AppDelegate.h”

#import “ViewController.h”

– (void)loadAdBanner {

  AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

  ViewController *vc = (ViewController *)delegate.window.rootViewController;

  [vc initADBanner];

}

저작자 표시
비영리
Array