creator 构建ios 项目 接入 google admob 横幅广告

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010971754/article/details/85654023

最近研究了基于cocos creator 构建的 ios项目接入google的广告。

Admob应用广告申请设置

1、在admob网站注册帐号等。 https://apps.admob.com/
2、在登录后点击 通过新的应用获利 按钮即可创建新的平台广告位。
3、添加完对应广告位后即可在 管理您的应用 按钮中找到添加的项目, 点击后可以查看应用广告具体的信息。
申请后可以得到一个 adUnitID,这在后面代码中需要用到。即下图的广告单元ID。

官方文档的两种做法:
1.用布局文件.storyboard
2.用代码来实现

这里本人对布局文件不熟,直接用的代码来实现。
这里首先创建一个UIViewController 命名GoogleAdmob,然后会创建出两个文件.h和.mm文件

然后呢,下载官方例子,将GoogleMobileAds.frameword 框架复制到ios项目中,然后导入。

具体的可以到 http://www.wicocos.cn/?id=29 详看

这里就只上代码:

#import <UIKit/UIKit.h>
@class GADBannerView;
@interface GoogleAdmob : UIViewController
@property(nonatomic, weak) IBOutlet GADBannerView *bannerView;
-(void)InitAdmob:(UIViewController*)_view;
@end
#import "GoogleAdmob.h"
#import "GoogleDefine.h"
@import GoogleMobileAds;
@interface GoogleAdmob ()
@end
@implementation GoogleAdmob
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
-(void)InitAdmob:(UIViewController*)_view
{
    CGSize size = [[UIScreen mainScreen]bounds].size;
    //这里的布局是页面底部
    self.bannerView = [[GADBannerView alloc]
                       initWithFrame:CGRectMake(20, size.height - 50, 320, 50)];
    self.bannerView.adUnitID = GADAdmobID;//这里的这个是申请的uid唯一标识符
    self.bannerView.rootViewController = _view;
    [self.view addSubview:self.bannerView];
    [self.bannerView loadRequest:[GADRequest request]];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
@end

然后我的做法,是对RootViewController.mm文件进行修改

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
    //这里加载底部横幅广告
    GoogleAdmob * admob = [[GoogleAdmob alloc]init];
    [admob InitAdmob:self];
    [self.view addSubview:admob.view];
}

到这里就完成了 

猜你喜欢

转载自blog.csdn.net/u010971754/article/details/85654023