`
bengan
  • 浏览: 200189 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

iPhone上根据不同的国家显示广告

 
阅读更多
本段教程和代码例子由 CocoaChina 会员“巫山冰”分享,可以自动识别 iPhone 所在地区,如果是美、日、英、法、德等支持 iAd 广告的国家,显示 iAd 广告;其它地区则显示 AdMob 广告。
部署广告函数
本函数在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;
    }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics