1、广告不是必须的,如果存在,就必须设计合理。iOS并没有禁止添加广告,但使用某些广告可能会导致审核无法通过。这里介绍iAd,adMob。
2、关于iAd,直接导入iAd库就可以了,iAd广告很多,横幅、插屏、视频广告都有,但我这里测试只有横幅是正常的,其他都没加载出来,可能是网速原因,也可能是本人过菜。
所以,iAd只采用横幅广告。(从官网下载的插屏广告demo也时常不能显示出广告来,故放弃之)
(1)添加iAd.framework
(2)在AppController.h,
@H_404_11@#import <iAd/iAd.h>
@interface AppController : NSObject <UIAccelerometerDelegate,...,ADBannerVIEwDelegate>
(3)在@end前添加一个属性
@property (nonatomic,strong)ADBannerVIEw *iadBanner;
(4)实现某些方法
-(voID)bannerVIEwWillLoadAd:(ADBannerVIEw *)banner{ }-(voID)bannerVIEwDIDLoadAd:(ADBannerVIEw *)banner{ }-(voID)bannerVIEw:(ADBannerVIEw *)banner dIDFailToReceiveADWithError:(NSError *)error{ }-(voID)showIADBanner{ self.iadBanner=[[ADBannerVIEw alloc]initWithFrame:CGRectMake(0,320,50)];// self.iadBanner.requiredContentSizeIDentifIErs=[NSSet setWithObjects:ADBannerContentSizeIDentifIErPortrait,nil]; self.iadBanner.currentContentSizeIDentifIEr=ADBannerContentSizeIDentifIErPortrait; self.iadBanner.delegate=self; [vIEwController.vIEw addSubvIEw:self.iadBanner];}(5)在
dIDFinishLaunchingWithOptions函数的return YES前调用[self showIADBanner];
3、使用adMob的横幅广告
(1)下载广告库,下载后目录如下:
(2)将GoogleMobileAds.framework拖进项目中,另外,再添加一些其他库,具体要哪些,不清楚。给个参考,可以用的。如下
(3)同样的,#import <GoogleMobileAds/GADBannerVIEw.h>
在AppController.h添加协议<GADBannerVIEwDelegate>
添加属性
@property(nonatomic,strong)GADBannerVIEw *gadBanner;
(4)在AppController.mm中添加方法
- (GADRequest*)createGADRequest{ GADRequest*request=[GADRequest request]; //下面是开启测试模式,上线后注释掉 request.testDevices=@[@"c85d7e50958ce70a91506be9c576882a",];//后面可以添加更多设备 return request;}- (voID)adVIEwDIDReceiveAd:(GADBannerVIEw *)bannerVIEw{ NSLog(@"ads");}- (voID)adVIEw:(GADBannerVIEw *)bannerVIEw dIDFailToReceiveADWithError:(GADRequestError *)error{ NSLog(@"Fail ads:%@",[error localizedFailureReason]);}-(voID)showAdmobBanner{ CGPoint origin=CGPointMake(0.0,vIEwController.vIEw.frame.size.height-kGADAdSizeBanner.size.height); self.gadBanner=[[GADBannerVIEw alloc] initWithAdSize:kGADAdSizeBanner origin:origin]; self.gadBanner.adUnitID=@"ca-app-pub-6686321446344812/4987791289"; self.gadBanner.delegate=self; self.gadBanner.rootVIEwController=vIEwController; [vIEwController.vIEw addSubvIEw:self.gadBanner]; [self.gadBanner loadRequest:[self createGADRequest]];}(5)直接调用 [self showAdmobBanner ];
4、使用adMob插屏广告
(1)AppController.h,
@H_404_11@#import <GoogleMobileAds/GADInterstitial.h>
添加< GADInterstitialDelegate >,添加属性
)GADInterstitial *gadInterstitial;
(2)实现方法-(voID)interstitial:(GADInterstitial *)ad dIDFailToReceiveADWithError:(GADRequestError *)error{ NSLog(@"Fail ads:%@",[error localizedFailureReason]);}-(voID)interstitialDIDReceiveAd:(GADInterstitial *)ad{ [self.gadInterstitial presentFromrootVIEwController:vIEwController];}-(voID)interstitialWilldismissScreen:(GADInterstitial *)ad{ }-(voID)showAdmobInterstitial{ if(self.gadInterstitial.isReady) { [self.gadInterstitial presentFromrootVIEwController:vIEwController]; }else{ self.gadInterstitial=[[[GADInterstitial alloc]initWithAdUnitID:@"ca-app-pub-6686321446344812/3511058088"]autorelease]; self.gadInterstitial.delegate=self; [self.gadInterstitial loadRequest:[self createGADRequest]]; }}(3)直接调用[self showAdmobInterstitial];
效果如下
总结
以上是内存溢出为你收集整理的cocos2d iOS添加广告全部内容,希望文章能够帮你解决cocos2d iOS添加广告所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)