將iAd框架添加到您的依賴項中。
在你的遊戲場景你的頭文件,添加ADBannerViewDelegate,例如:
@interface MainScene : CCNode <CCPhysicsCollisionDelegate, ADBannerViewDelegate>
在實現文件中,添加實例變量_bannerView:
@implementation MainScene {
ADBannerView *_bannerView;
}
最後,插入iAD代碼(帶有一些cocos2d調整)。這是我的肖像模式遊戲的實施與頂部橫幅。沒有隱藏方法,但它很容易實現。
# pragma mark - iAd code
-(id)init
{
if((self= [super init]))
{
// On iOS 6 ADBannerView introduces a new initializer, use it when available.
if ([ADBannerView instancesRespondToSelector:@selector(initWithAdType:)]) {
_adView = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];
} else {
_adView = [[ADBannerView alloc] init];
}
_adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifierPortrait];
_adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
[[[CCDirector sharedDirector]view]addSubview:_adView];
[_adView setBackgroundColor:[UIColor clearColor]];
[[[CCDirector sharedDirector]view]addSubview:_adView];
_adView.delegate = self;
}
[self layoutAnimated:YES];
return self;
}
- (void)layoutAnimated:(BOOL)animated
{
// As of iOS 6.0, the banner will automatically resize itself based on its width.
// To support iOS 5.0 however, we continue to set the currentContentSizeIdentifier appropriately.
CGRect contentFrame = [CCDirector sharedDirector].view.bounds;
if (contentFrame.size.width < contentFrame.size.height) {
_bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
} else {
_bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape;
}
CGRect bannerFrame = _bannerView.frame;
if (_bannerView.bannerLoaded) {
contentFrame.size.height -= _bannerView.frame.size.height;
bannerFrame.origin.y = contentFrame.size.height;
} else {
bannerFrame.origin.y = contentFrame.size.height;
}
[UIView animateWithDuration:animated ? 0.25 : 0.0 animations:^{
_bannerView.frame = bannerFrame;
}];
}
- (void)bannerViewDidLoadAd:(ADBannerView *)banner {
[self layoutAnimated:YES];
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
[self layoutAnimated:YES];
}
我感謝上帝在這個世界上像你這樣的人。我一整天都在搜索如何做到這一點,所有我發現是複雜的代碼。然而,當我看到這個時,我就像是太容易了。非常感謝你。 – Crazycriss
謝謝:)我將它變成了一個幫助程序實現,使實現超級易於使用:https://github.com/svenanders/iAdHelper – Sven