控制台中没有错误,似乎一切正常。但bannerView_只是没有显示。
这是我的AdMobObject.h
#import "AdMobObject.h"
#import "RootViewController.h"
#import "GADBannerView.h"
@class RootViewController;
@class GADBannerView;
@interface AdMobObject : UIViewController{
RootViewController * viewController;
GADBannerView * bannerView_;
}
+ (AdMobObject *) shared;
- (void) addAdMob;
- ( void) hideAdMob;
@end
这是我的AdMobObject.mm
#import "AdMobObject.h"
#import "AppController.h"
#import "RootViewController.h"
#import "EAGLView.h"
#import "cocos2d.h"
@implementation AdMobObject
static AdMobObject* instance;
+(AdMobObject *) shared{
@synchronized(self){
if( instance == nil ){
instance = [[self alloc] init];
}
}
return instance;
}
- (void) addAdMob{
NSLog(@"----------addAdMob");
bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerPortrait];
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenHeight = screenRect.size.height;
CGFloat screenWidth = screenRect.size.width;
viewController.view.frame = CGRectMake(0,0,screenWidth,screenHeight);
[bannerView_ setFrame:CGRectMake(0,
screenHeight-bannerView_.bounds.size.height,
//0,
bannerView_.bounds.size.width,
bannerView_.bounds.size.height)];
bannerView_.adUnitID = @"myadmobid";
bannerView_.rootViewController = viewController;
[viewController.view addSubview:bannerView_];
GADRequest *request = [GADRequest request];
// For testing
request.testDevices = [NSArray arrayWithObjects:@"mydeviceid", nil];
[bannerView_ loadRequest:request];
[viewController.view addSubview:bannerView_];
}
- (void) showAdMob{
}
- (void) hideAdMob{
[bannerView_ setHidden:YES];
}
@end
然后我有一个课程来管理它:
@interface MyGameCenterManager : NSObject
{
}
+(MyGameCenterManager *) shared;
+(void) addAdMob;
+(void) hideAdMob;
@end
这是类实现:
@implementation MyGameCenterManager
static MyGameCenterManager *instance;
+(MyGameCenterManager*)shared{
@synchronized(self){
if(instance == nil){
instance = [[self alloc] init];
}
}
return instance;
}
+ (void) addAdMob
{
[[AdMobObject shared] addAdMob];
}
+ (void) hideAdMob{
[[AdMobObject shared] hideAdMob];
}
最后,我有cpp类:
#include "ScriptingCore.h"
namespace ls{
class GameCenterBridge: public cocos2d::CCObject{
public:
static cocos2d::CCScene* scene();
virtual bool init();
CREATE_FUNC(GameCenterBridge);
void addAdMob();
void hideAdMob();
};
}
我打电话给我的班级:
ls :: GameCenterBridge * class = new ls :: GameCenterBridge(); 类 - > addAdMob();
我可以看到控制台日志:
---------- addAdMob
表示它进入addAdMob函数。并且没有其他错误。
但横幅视图并没有显示出来。
我展示的唯一方法是在didFinishLaunchingWithOptions中将代码添加到AppController.mm中。但我只是想知道为什么它不能为我自己创造的课程而工作。
答案 0 :(得分:0)
当我实施AdMob时,我只在从服务器加载广告时展示广告。您可以通过实现GADBannerViewDelegate
来实现这一目标,尤其是有趣的方法
- (void)adViewDidReceiveAd:(GADBannerView *)bannerView;
- (void)adView:(GADBannerView *)bannerView didFailToReceiveAdWithError:(GADRequestError *)error
这样您就可以确定收到了广告。