在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