1. first of all, you should setup iAd in the iTunes Connect. Just need some clicks.It's very easy.
2. You should Create AdBannerView in viewDidLoad in the UntitledViewController.m
#import <IAd/IAd.h>
ADBannerView *adView;
you should add this two lines to UntitledViewController.h. one is a variable in UntitledViewController.
follow is the code which should be added into viewDidLoad.
// iAd
adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifierPortrait];
adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
// use device resolution
char* szTemp = agk::GetDeviceName();
char szName[64];
strcpy(szName, szTemp);
delete szTemp;
int iCmp = strcmp(szName, "ios|iPa");
CGSize windowSize;
if (7==iCmp || 14==iCmp) // iphone
{
windowSize.width = 480;
windowSize.height = 320;
}
else if(100==iCmp) // ipad
{
windowSize.width = 1024;
windowSize.height = 768;
}
else
{
windowSize.width = 480;
windowSize.height = 320;
}
// frame 66 768
adView.transform = CGAffineTransformMakeRotation ( DEGREES_TO_RADIANS ( 90 ) );
adView.center = CGPointMake ( adView.frame.size.width / 2, windowSize.width / 2 );
[self.view addSubview:adView];
adView.delegate = self;
adView.hidden = YES;
ADBannerContentSizeIdentifierPortrait is iAd orientation.You should change it if your game is in the other mode.
then , you should calculate the window size of iAd by Device. Then make a transform to AdBannerView.
when you create the AdBannerView, you should hide it. Because maybe the iAd request will fail. The Apple Inc. don't want to see a white window in your games.
3.You should add delegate to UntitledViewController as follow code.
@interface UntitledViewController : UIViewController<ADBannerViewDelegate, UIActionSheetDelegate, SKProductsRequestDelegate>
then, add two variables in UntitledViewController
@property (nonatomic,assign) BOOL bannerIsVisible;
@property (nonatomic,assign) BOOL bannerLoaded;
then you should add some functions about AdBannerView.
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
if (self.bannerIsVisible)
{
adView.hidden = YES;
self.bannerIsVisible = NO;
}
}
- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
NSLog(@"should begin");
return YES;
}
- (void)bannerViewActionDidFinish:(ADBannerView *)banner
{
NSLog(@"did finish");
}
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
NSLog(@"%d",adView.bannerLoaded);
if (g_Game->m_eGameMode!=eGameMenu && g_Game->m_eGameMode!=eGameScene && !g_Game->m_bRemovedAds)
{
adView.hidden = NO;
}
else
{
bannerLoaded = YES;
}
NSLog(@"did load");
}
Pay attention to these two functions:
one, didFailToReceiveAdWithError, you should hide the AdBannerView, if there is a error about it.
two,bannerViewDidLoadAd, it means the ads is ready, you can show or hide it by your game state.
4. add your function to UntitledViewController : - (void) showAdv
BOOL)bShow;
- (void) showAdv:(BOOL)bShow
{
if (bannerLoaded)
{
bannerIsVisible = bShow;
adView.hidden = !bShow;
}
}
You can call this function in your cpp file. Then you can control the ads show or hide.
That's all. It's very easy, right?
Any questions can follow this thread. I'll answer as quickly as I can.
Halley
The Miracrea Games