Cocos2d-x   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了cocos2d iOS添加广告大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

1、广告不是必须的,如果存在,就必须设计合理。iOS并没有禁止添加广告,但使用某些广告可能会导致审核无法通过。这里介绍iAd,adMob。

2、关于iAd,直接导入iAd库就可以了,iAd广告很多,横幅、插屏、视频广告都有,但我这里测试只有横幅是正常的,其他都没加载出来,可能是网速原因,也可能是本人过菜。

所以,iAd只采用横幅广告。(从官网下载的插屏广告demo也时常不能显示出广告来,故放弃之)

(1)添加iAd.framework

(2)在AppController.h,

#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,

#import <GoogleMobileAds/GADInterstitial.h>

添加< GADInterstitialDelegate >,

添加属性

:11px; line-height:normal; font-family:Menlo; color:rgb(187,retain)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添加广告所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。