iOS
发布时间:2022-03-31 发布网站:大佬教程 code.js-code.com
大佬教程收集整理的这篇文章主要介绍了分享AdMob + iAd部署代码,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
分享AdMob + iAd部署代码
http://www.cocoachina.com/bbs/read.php?tid-43249-keyword-admob.html
之前我的
AdMob广告
显示一直采用IBSupport模式,这两天搞搞In App Purchase(程序内购买),又重新研究和测试了
AdMob代码,终于可以丢掉IBSupport模式,进入全
代码控制了。
论坛内
有人建议用AdWhirl实现
AdMob +
iAd,还没有来得急研究,不知道难度大不大。
在这里
分享一下我的最终方案,不知道是否还有人需要
在这方面努力,同时尚未能够全面测试,不知道还存在哪些
问题,有经验的朋友交流交流一下。
部署
广告函数,本
函数在viewDidLoad()中
调用,
同时也可以在购买“广告
去除”成功后
调用:
- (
void)disposeAds
{
BOOL is_iAdON =
NO;
BOOL is_adMobON =
NO;
//分析设备可
显示哪一家广告
if
([[[
UIDevicecurrentDevice
] @H_974_6@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;
}
}
}
else
if
([[[
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
] @H_974_6@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
[
adMobAd
retain
]; // 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
] @H_974_6@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
] @H_974_6@model
] rangeOfString
:
@"iPad"
].
LOCATIOn
==
0
)
iAdView.
frame =
CGRectOffset(
iAdView.
frame,
0,
66);
else
iAdView.
frame =
CGRectOffset(
iAdView.
frame,
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
] @H_974_6@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
] @H_974_6@model
] rangeOfString
:
@"iPad"
].
LOCATIOn
==
0
)
bAnner.
frame =
CGRectOffset(bAnner.
frame,-
66);
else
bAnner.
frame =
CGRectOffset(bAnner.
frame,-
50);
[
UIViewcommitAnimations
];
bAnnerIsVisible
=
YES
;
bAnner.
hidden =
NO;
}
}
增加个Demo,解压后,工程文件在Hello Arifly文件夹内,外面的
AdMob和TouchJSON为
AdMob SDK附带的源码,需要引用到。
![分享AdMob + iAd部署代码 分享AdMob + iAd部署代码](http://code.js-code.com/res/2020/06-28/07/4e476353a50e0a2cd951ce018f5dcc69.gif)
Ads(AdMob+iAd)Demo.zip (1549 K) 下载次数:452
大佬总结
以上是大佬教程为你收集整理的分享AdMob + iAd部署代码全部内容,希望文章能够帮你解决分享AdMob + iAd部署代码所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。