在cocos2d里添加iad

iPhone程序中集成iAd广告 是本文要介绍的内容,iAd的推出无疑给Iphone/IPad的应用程序开发者打开了另一条创收之门,前不久,美国的一位开发者Json Ting开发的将相机闪光灯转为手电筒的应用,集成iAd后在第一天就给他带来了1400$的广告收入。我将在这篇文章中讲讲如何把iAd集成到你的应用程序中。另外也会提到集成中可能遇到的一些问题:

如何保持与os 3.0的后向兼容。

与UITableViewController的集成。

1、将Base SDK设为4.0, 将Deployment target设为3.0. 如图所示:

2、链接iAd Framework.

右击Frameworks, 选择"Add\Existing Frameworks", 添加"iAd.framework". 但是在没有iAd.framework的机器上,比如3.x版本的,这样会Crash.所以要把这个链接变为weak link. 在"targets"中右击你的工程,然后"Get Info", 在Linking\Other Linker Flags里添加"-weak_framework iAd". 这样就能够保证程序的后向兼容性。


直接代码上

头文件

//
//  bluetoothSceneConnect.h
//  pigbon
//
//  Created by Mica001 on 11-12-1.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import  <iAd/iAd.h>
@interface bluetoothSceneConnectScene : CCScene {
    
}
+(id) ShowScene;

@end

@interface bluetoothSceneConnect : CCLayer <ADBannerViewDelegate>{
    ADBannerView *adView; 
}
@end
实现在文件


//
//  bluetoothSceneConnect.m
//  pigbon
//
//  Created by Mica001 on 11-12-1.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "bluetoothSceneConnect.h"
#import "SingletonGameState.h"
#import "ui1Scene.h"
#import "Game2Scene.h"
@implementation bluetoothSceneConnectScene

+(id) ShowScene
{
	bluetoothSceneConnectScene *scene = [bluetoothSceneConnectScene node];
	return scene;
}
-(id) init
{
	if( (self=[super init] )) 
	{
		bluetoothSceneConnect *glayer = [bluetoothSceneConnect node];
		[self addChild: glayer];
	}
	return self;
}


- (void) dealloc
{
	[super dealloc];
}

@end


@implementation bluetoothSceneConnect

-(id) init
{
	if( (self=[super init])) 
    {
        self.isTouchEnabled = YES;
        
        Class classAdBannerView = NSClassFromString(@"ADBannerView");
		if (classAdBannerView != nil)
		{
			adView = [[classAdBannerView alloc] init];
            [adView setFrame:CGRectMake(0, 0, 480, 32)];
            
			[adView setDelegate:self];
            //把iap添加到scene上
            [[[CCDirector sharedDirector] openGLView] addSubview:adView];
		}        

    }
    return self;
}

//这里是iad 回调
#pragma mark  iad
- (void)adAvailabilityDidChange {
    NSLog(@"[iAd]: Ads are available! Let's display one!");
}

- (void)cancelBannerViewAction {
    NSLog(@"Banner was cancelled!");
}
- (void)bannerViewDidLoadAd:(ADBannerView *)banner {
    NSLog(@"[iAd]: Ad did load.");
}
- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave {
    NSLog(@"[iAd]: An action was started from the banner. Application will quit: %d", willLeave);
    return YES;
}
- (void)bannerViewActionDidFinish:(ADBannerView *)banner {
    NSLog(@"[iAd]: Action finished.");
}
- (void)bannerView:(ADBannerView *) didFailToReceiveAdWithError:(NSError *)error {
    NSLog(@"[iAd]: Faild to load the banner: %@", error);
}
#pragma mark  iad_end




-(void)connectionCompletion{
    //NSLog(@"connectionCompletion!");
    [[CCDirector sharedDirector]replaceScene:[CCTransitionSlideInL transitionWithDuration:0.8f scene:[Game2Scene node]] ];
}

-(void)dealloc{
    //这个一定要添加 把iad 删除了
    [adView removeFromSuperview];
    [super dealloc];
}
@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值