Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5424190
  • 博文数量: 763
  • 博客积分: 12108
  • 博客等级: 上将
  • 技术积分: 15717
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-28 21:21
个人简介

业精于勤,荒于嬉

文章分类

全部博文(763)

文章存档

2018年(6)

2017年(15)

2016年(2)

2015年(31)

2014年(14)

2013年(87)

2012年(75)

2011年(94)

2010年(190)

2009年(38)

2008年(183)

2007年(28)

分类: C/C++

2011-02-18 14:09:09



分享AdMob + iAd部署代码   

之前我的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= [[ADBannerViewalloc] initWithFrame:CGRectZero]; 
iAdView.requiredContentSizeIdentifiers= [NSSetsetWithObject: 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 
CGRectframe = 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]; 
[adMobRefreshTimerinvalidate]; 
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重新编辑 ]
阅读(1740) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~