之前我的
AdMob广告显示一直采用IBSupport模式,这两天搞搞In App Purchase(程序内购买),又重新研究和测试了
AdMob代码,终于可以丢掉IBSupport模式,进入全
代码控制了。
论坛内有人建议用AdWhirl实现
AdMob +
iAd,还没有来得急研究,不知道难度大不大。在这里
分享一下我的最终方案,不知道是否还有人需要在这方面努力,同时尚未能够全面测试,不知道还存在哪些
问题,有经验的朋友交流交流一下。
部署
广告函数,本函数在viewDidLoad()中调用,同时也可以在购买“广告去除”成功后调用:
- (
void)disposeAds {
BOOL is_iAdON =
NO;
BOOL is_adMobON =
NO;
//分析设备可显示哪一家广告
if([[[UIDevicecurrentDevice
] model
] rangeOfString
:@"iPad"].location== 0) { is_adMobON =
YES;
if([[[UIDevicecurrentDevice
] systemVersion
] floatValue
] >= 4.2f) { //为iPad 4.2之后的系统显示iAd广告
//如果是北
美国时间或太平洋时间,则假想是美国用户
//2010.12 英国,法国
//2011.1 德国
//2011.? 日本
if([[[NSTimeZonelocalTimeZone
] name
] rangeOfString
:@"America/"].location== 0 || [[[NSTimeZonelocalTimeZone
] name
] rangeOfString
:@"Pacific/"].location== 0 || [[[NSTimeZonelocalTimeZone
] name
] rangeOfString
:@"Europe/"].location== 0 || [[[NSTimeZonelocalTimeZone
] name
] rangeOfString
:@"Asia/Tokyo"].location== 0) {
is_adMobON =
NO;
}
}
}
elseif([[[UIDevicecurrentDevice
] systemVersion
] floatValue
] >= 4.0f) { //如果是北美国时间或太平洋时间,则假想是美国用户
//2010.12 英国,法国
//2011.1 德国
//2011.? 日本
if([[[NSTimeZonelocalTimeZone
] name
] rangeOfString
:@"America/"].location== 0 || [[[NSTimeZonelocalTimeZone
] name
] rangeOfString
:@"Pacific/"].location== 0 || [[[NSTimeZonelocalTimeZone
] name
] rangeOfString
:@"Europe/"].location== 0 || [[[NSTimeZonelocalTimeZone
] name
] rangeOfString
:@"Asia/Tokyo"].location== 0) {
is_adMobON =
NO;
}
else
is_adMobON =
YES;
}
else
is_adMobON =
YES;
if(!is_adMobON)
is_iAdON =
YES;
//检测购买(这个为程序内购买了“去除广告”功能的把广告清除或是不显示,removeAdPurchased是个变量,本文未讨论)
if(removeAdPurchased
) { is_adMobON =
NO;
is_iAdON =
NO;
}
//打开广告
if(is_adMobON || is_iAdON) {
if(is_adMobON) {
//启用
AdMob if(!
adMobAd) {
CGSize sizeToRequest;
if([[[UIDevicecurrentDevice
] model
] rangeOfString
:@"iPad"].location== 0) sizeToRequest = CGSizeMake(748, 110); else sizeToRequest = CGSizeMake(320, 48); adMobAd = [
AdMobView requestAdOfSize:sizeToRequest
withDelegate:
self];
// start a new ad request [adMobAdretain]; // this will be released when it loads (or fails to load)
}
}
else {
//启用iAd
if(!
iAdView) {
iAdView= [[ADBannerView
alloc] initWithFrame:CGRectZero
]; iAdView.requiredContentSizeIdentifiers
= [NSSet
setWithObject: ADBannerContentSizeIdentifierPortrait
]; iAdView.currentContentSizeIdentifier
= ADBannerContentSizeIdentifierPortrait
; if([[[UIDevicecurrentDevice
] model
] rangeOfString
:@"iPad"].location== 0) iAdView.frame = CGRectMake(0, 1024, 768, 66); else iAdView.frame = CGRectMake(0, 480, 320, 50); iAdView.
delegate =
self;
[
self.
view addSubview:
iAdView];
iAdView.hidden= YES; //暂时不显示广告框,收到广告后再显示出来
}
}
}
else
{ //关闭广告
if(adMobAd
) { //关闭
AdMob [adMobAdremoveFromSuperview
]; [
adMobAd release];
adMobAd =
nil;
}
else if(
iAdView) {
//关闭iAd(bannerIsVisible是个变量,用于标识iAd广告是否已经显示内容,可不用)
if(bannerIsVisible
) { [UIViewbeginAnimations
:@"animateAdBannerOff"context
:NULL]; if([[[UIDevicecurrentDevice
] model
] rangeOfString
:@"iPad"].location== 0) iAdView.
frame =
CGRectOffset(
iAdView.
frame,
0,
66);
else iAdView.
frame =
CGRectOffset(
iAdView.
frame,
0,
50);
[UIViewcommitAnimations
]; bannerIsVisible
= NO; iAdView.
hidden =
YES;
}
[iAdViewremoveFromSuperview
]; [
iAdView release];
iAdView =
nil;
}
}
}
AdMob广告收到内容后处理函数:
// Sent when an ad request loaded an ad; this is a good opportunity to attach
// the ad view to the hierachy.
- (
void)didReceiveAd:(
AdMobView *)adView {
// get the view frame
CGRect
frame = self.view
.frame
; // put the ad at the bottom of the screen
if([[[UIDevicecurrentDevice
] model
] rangeOfString
:@"iPad"].location== 0) adView.
frame =
CGRectMake((frame.
size.
width -
748)/
2, frame.
size.
height -
110,
748,
110);
else
adView.
frame =
CGRectMake(
0, frame.
size.
height -
48, frame.
size.
width,
48);
[self.viewaddSubview
:adView]; [adMobRefreshTimer
invalidate]; adMobRefreshTimer
= nil; adMobRefreshTimer= [NSTimerscheduledTimerWithTimeInterval
:AdMob_REFRESH_PERIODtarget
:selfselector
:@selector(refreshAdMob:) userInfo
:nilrepeats
:YES]; }
iAd广告收到内容后处理函数:
- (
void)bannerViewDidLoadAd:(
ADBannerView *)banner {
if(!bannerIsVisible
) {
[UIViewbeginAnimations
:@"animateAdBannerOn"context
:NULL]; if([[[UIDevicecurrentDevice
] model
] rangeOfString
:@"iPad"].location== 0) banner.
frame =
CGRectOffset(banner.
frame,
0, -
66);
else
banner.
frame =
CGRectOffset(banner.
frame,
0, -
50);
[UIViewcommitAnimations
]; bannerIsVisible
= YES; banner.
hidden =
NO;
}
}
增加个Demo,解压后,工程文件在Hello Arifly文件夹内,外面的
AdMob和TouchJSON为
AdMob SDK附带的源码,需要引用到。
(1549 K) 下载次数:285 [ 此帖被巫山冰在2011-01-03 17:55重新编辑 ]