cocos2dx添加Admob广告中遇到的问题

1、添加库CoreData.framework,AdSupport.framework,StoreKit.framework,SystemConfiguration.framework,MessageUI.framework;

先加入以上这么些,然后在 AppController.mm里面 viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
viewController.wantsFullScreenLayout = YES;
viewController.view = __glView;

//------------------- Add Admob
GADBannerView * bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerPortrait];

bannerView_.rootViewController = viewController;

bannerView_.adUnitID =
"ca-app-pub-2174847275118262/6657323033";

GADRequest *request = [GADRequest request];

request.testing = NO;

// Set RootViewController to window
if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0)
{
// warning: addSubView doesn't work on iOS6
[window addSubview: viewController.view];
}
else
{
// use this method on ios6
[window setRootViewController:viewController];
}

[bannerView_ setFrame:CGRectMake(0, viewController.view.frame.size.height - bannerView_.frame.size.height, bannerView_.frame.size.width, bannerView_.frame.size.height)];
[viewController.view addSubview: bannerView_];
[viewController.view bringSubviewToFront:bannerView_];
[bannerView_ loadRequest: request];

[window makeKeyAndVisible];

[[UIApplication sharedApplication] setStatusBarHidden: YES];


2、运行出现错误

iOS - _OBJC_CLASS_$_CTTelephonyNetworkInfo not found?


添加库 CoreTelephony.framework解决。

3、继续运行 控制台报错

cocos2dx添加Admob广告中遇到的问题_第1张图片

查找资料发现应该在build setting中的添加-ObjC



完成,广告条正常显示。


4、插屏广告

添加一个oc文件命名AdMobInstritial

头文件代码:

#import
#import
#import "GADInterstitial.h"
#import "GADInterstitialDelegate.h"

@interface InterAdmobViewController : UIViewController

@property(nonatomic, retain) GADInterstitial *interstitial;

@end

实现文件代码:

#import "AdMobInstritial.h"

@implementation InterAdmobViewController

- (void)viewDidLoad
{
[super viewDidLoad];

self.interstitial = [[GADInterstitial alloc] init];

self.interstitial.delegate = self;

self.interstitial.adUnitID = @"ca-app-pub-4305631875686357/9062669688";

[self.interstitial loadRequest: [self createRequest]];
}

- (GADRequest *)createRequest {
GADRequest *request = [GADRequest request];

// Make the request for a test ad. Put in an identifier for the simulator as
// well as any devices you want to receive test ads.
request.testDevices =
[NSArray arrayWithObjects:
// TODO: Add your device/simulator test identifiers here. They are
// printed to the console when the app is launched.
nil];
return request;
}

- (void)interstitialDidReceiveAd:(GADInterstitial *)interstitial {
[interstitial presentFromRootViewController:self];
}

@end

添加一个混编广告管理类AdManager

AdManager *AdManager::sharedInstance() {
static AdManager *manager = NULL;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = new AdManager;
});
return manager;
}

void AdManager::showAdmobInterstitial()
{
InterAdmobViewController* vc = [[InterAdmobViewController alloc] init];

//myoc是一个单例类 用来保存根视图
[MyOc::shared()->view addSubview:vc.view];
}

在需要添加插屏的地方调用接口即可完成!



你可能感兴趣的:(cocos2dx,iOS)