why my admob doesn't show? - c++

There is no errors in console, and seems everything work. But the bannerView_ just didn't show.
here is my 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
here is my 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
Then i have a class to manage it:
#interface MyGameCenterManager : NSObject
{
}
+(MyGameCenterManager *) shared;
+(void) addAdMob;
+(void) hideAdMob;
#end
here is the class implementation:
#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];
}
Finally, I have cpp class:
include "cocos2d.h"
#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();
};
}
I called my class:
ls::GameCenterBridge * class = new ls::GameCenterBridge();
class->addAdMob();
I can see the console log:
----------addAdMob
which means it enter into the addAdMob function. And there has no other errors.
But the banner view just didn't show.
the only way I make it show is to add codes into AppController.mm in didFinishLaunchingWithOptions. But I am just wondering why it won't work for my self created class.

When i was implementing AdMob i showed the ad only when it got loaded from the server. You can achieve this by implementing GADBannerViewDelegate, especially interesting methods are
- (void)adViewDidReceiveAd:(GADBannerView *)bannerView;
- (void)adView:(GADBannerView *)bannerView didFailToReceiveAdWithError:(GADRequestError *)error
This way you can be sure that you received the ad.

Related

why I get error when call [myclass alloc] misx c++ to object-c++ to object-c?

i try use call c++ from object c
use B4I
this code but i get error
i already use file.mm
this file Greeting.cpp
#include "Greeting.hpp"
Greeting::Greeting() {
greeting = "Hello C++!";
}
std::string Greeting::greet() {
return greeting;
}
this file Greeting.hpp
#ifndef Greeting_hpp
#define Greeting_hpp
#include <stdio.h>
#include <string>
class Greeting {
std::string greeting;
public:
Greeting();
std::string greet();
};
#endif /* Greeting_hpp */
this file ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
- (void)showGreeting;
#end
this file ViewController.mm
#import "ViewController.h"
#import "Greeting.hpp"
#interface ViewController ()
{
Greeting greeting;
IBOutlet UIButton *helloButton;
}
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)showGreeting {
NSLog(#"test %#");
}
#end
i try call it
ViewController* ViewController1= [[ViewController alloc] init];
[ ViewController1 showGreeting ];
i get error unknown method
-------------------------------
Undefined symbols for architecture arm64:
"_OBJC_CLASS_$_ViewController", referenced from:
objc-class-ref in b4i_main.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
---------------------------------
when i use
ViewController* ViewController1;
[ ViewController1 showGreeting ];
return null
note this example HelloCpp-master
thanks
The following example shows how to call a c++ class from an objc app using a method in AppDelegate. It may be run in Terminal by first saving the source code in a file named 'greet.mm' and then using this command: clang greet.mm -framework Cocoa -o greet && ./greet
//clang greet.mm -framework Cocoa -o greet && ./greet
#import <Cocoa/Cocoa.h>
class Greeting {
public:
void greet();
};
void Greeting::greet() {
NSLog(#"Hello C++!");
}
#interface AppDelegate : NSObject <NSApplicationDelegate>
{
NSWindow *window;
}
- (void) myBtnAction: (id)sender;
- (void) buildMenu;
- (void) buildWnd;
#end
#implementation AppDelegate
- (void) myBtnAction: (id)sender {
Greeting g;
g.greet();
}
- (void) buildMenu {
NSMenu *menubar = [NSMenu new];
NSMenuItem *menuBarItem = [NSMenuItem new];
[menubar addItem:menuBarItem];
[NSApp setMainMenu:menubar];
NSMenu *appMenu = [NSMenu new];
NSMenuItem *quitMenuItem = [[NSMenuItem alloc] initWithTitle:#"Quit"
action:#selector(terminate:) keyEquivalent:#"q"];
[appMenu addItem:quitMenuItem];
[menuBarItem setSubmenu:appMenu];
}
- (void) buildWnd {
#define _wndW 200
#define _wndH 150
window = [ [ NSWindow alloc ] initWithContentRect:NSMakeRect( 0, 0, _wndW, _wndH )
styleMask: NSWindowStyleMaskClosable | NSWindowStyleMaskTitled
backing:NSBackingStoreBuffered defer:YES];
[window center];
[window setTitle: #"Test window" ];
[window makeKeyAndOrderFront:nil];
// **** My Button **** //
NSButton *myBtn =[[NSButton alloc]initWithFrame:NSMakeRect( 40, 80, 125, 24 )];
[myBtn setBezelStyle:NSBezelStyleRounded ];
[myBtn setTitle: #"Run CPP Class"];
[myBtn setAction: #selector (myBtnAction:)];
[[window contentView] addSubview: myBtn];
// **** Quit btn **** //
NSButton *quitBtn = [[NSButton alloc]initWithFrame:NSMakeRect( _wndW - 50, 10, 40, 40 )];
[quitBtn setBezelStyle:NSBezelStyleCircular ];
[quitBtn setTitle: #"Q" ];
[quitBtn setAction:#selector(terminate:)];
[[window contentView] addSubview: quitBtn];
}
- (void) applicationWillFinishLaunching: (NSNotification *)notification {
[self buildMenu];
[self buildWnd];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
}
#end
int main () {
NSApplication *app = [NSApplication sharedApplication];
AppDelegate *appDelegate = [[AppDelegate alloc] init];
[app setDelegate:appDelegate];
[NSApp run];
return 0;
}
Should run in iOS. Change extension of ViewController from .m to .mm
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#end
#import "ViewController.h"
class Greeting {
public:
void greet();
};
void Greeting::greet() {
NSLog(#"Hello C++!");
}
#interface ViewController ()
#property (strong, nonatomic) IBOutlet UIButton *LaunchBtn;
#end
#implementation ViewController
- (IBAction)showGreeting:(id)sender {
Greeting g;
g.greet();
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
#end

Set viewController shared public object

I've got ViewController that contains tableView and an array of items to be displayed on that table.
In order to change table content, I could initialise the array and calling reloadData method from within ViewController button callback (see buttonTap method below).
However, I also need to change the table contents from external code outside the scope of viewController, but it's inaccessible.
In the minimal example below I'm using external C/C++ based thread that unsuccessfully attempts to access a viewController method but it fails on compilation due to No known class method for selector 'changeArr'
Any idea how to make viewController public ?
viewController.m
#import "ViewController.h"
#interface ViewController() <NSTableViewDelegate, NSTableViewDataSource>
#property (weak) IBOutlet NSTableView *tableView;
#property NSMutableArray<NSString*> * arr;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.tableView reloadData];
}
- (void)viewWillAppear {
[super viewWillAppear];
[self.tableView reloadData];
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
return self.arr.count;
}
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
NSTableCellView* cellView =(NSTableCellView*)[tableView makeViewWithIdentifier:#"cell1" owner:nil];
cellView.textField.stringValue = self.arr[row];
return cellView;
}
-(void)changeArr {
self.arr = [NSMutableArray arrayWithObjects:#"ccc", #"ddd", nil];
[self.tableView reloadData];
NSLog(#"fwefwe");
}
- (IBAction)buttonTap:(id)sender {
self.arr = [NSMutableArray arrayWithObjects:#"AAAA", #"BBB", nil];
[self.tableView reloadData];
}
main.m
#import <Cocoa/Cocoa.h>
#include <pthread.h>
#import "ViewController.h"
void updateTable() {
sleep(10);
dispatch_sync(dispatch_get_main_queue(), ^{ [ViewController changeArr]; }); // ERROR : No known class method for selector 'changeArr'
}
int main(int argc, const char * argv[]) {
#autoreleasepool {
// Setup code that might create autoreleased objects goes here.
}
pthread_t threadHandler;
pthread_create(&threadHandler, NULL, &updateTable, NULL);
return NSApplicationMain(argc, argv);
}
changeArr is a private function(-). So you can't call [ViewController changeArr].
If you change changeArr to public fuction(+), it's impossible to access internal array.
So I think the easy way to do this task is using a instance of ViewController by singletone.
...
#implementation ViewController
+ (id) sharedViewController {
static ViewController *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
sharedInstance = [storyboard instantiateViewControllerWithIdentifier:#"MyViewControllerIdentifier"];
});
return sharedInstance ;
}
- (void)viewDidLoad {
[super viewDidLoad];
...
and in updateTable:
dispatch_sync(dispatch_get_main_queue(), ^{ [[ViewController sharedViewController] changeArr]; });

objective-c to c - transferring data

Here's my code ----
//Cpp/Objective-C Wrapper function
iSketchnoteController * isknController;
extern "C"
{
void launchiSketchnoteController(SlateTableViewController *viewController)
{
try
{
if (viewController == nil) {
isknController = new
iSketchnoteController([MyManager sharedManager].deviceName);
} else {
isknController = new iSketchnoteController(viewController);
}
} catch (Error &err)
{
NSLog(#"Error");
}
}
My desired effect is to have [MyManager sharedManager] be my "static app state." Meaning, a management system throughout my entire app I can use for things like setting a device name, disconnecting it, reconnect it, etc...
Also, the above is a .mm file.
Anyway - here's how I have it implemented.
#import <Foundation/Foundation.h>
#import <CoreBluetooth/CoreBluetooth.h>
NS_ASSUME_NONNULL_BEGIN
#class SelectSlateTableViewController;
#protocol MySwiftProtocol;
#interface MyManager : NSObject
+ (instancetype)sharedManager;
#property (nonatomic, assign) NSString *deviceName;
#end
NS_ASSUME_NONNULL_END
and the .m file
#import "MyManager.h"
extern void disconnect();
extern void connectToPeripheral(CBPeripheral* peripheral);
extern void connectToPeripheralByName(NSString* name);
extern void launchiSketchnoteController(SelectSlateTableViewController *viewController);
extern void isConnected();
#implementation MyManager
+ (instancetype)sharedManager {
static MyManager *sharedMyManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedMyManager = [[self alloc] init];
});
return sharedMyManager;
}
- (instancetype)init
{
self = [super init];
return self;
}
#end
Essentialy, in the first code box [MyManager sharedManager].deviceName is NOT accessible.

Cocos2d: Controller calling method of a CCNode

My GameController.m calls a method of CCNode class. It seems to call it as I can see NSLog but the method is not either operate or not result in view. In other words, screen does not change when calling the method from GameController. (method operates itself in its own class)
How should I change my code to fix it?
Summary:
GameController -> calls -> GoalPost.GoalKeeper.method.
Code:
GameController.m, GoalPost.m and GoalKeeper.m
GameController.m
#import "GameController.h"
#import "cocos2d-ui.h"
#import "GoalPost.h"
#import "Ball.h"
#import "Field.h"
#interface GameController()
#property Field* field;
//#property Ball* ball;
#property GoalPost* post;
#end
#implementation GameController
- (instancetype)init
{
self = [super init];
if (self) {
self.field = [Field node];
// self.ball = [Ball node];
self.post = [GoalPost node];
}
return self;
}
//control computer.
-(void)kickerTurn{
NSLog(#"Player kick");
//random location generation
//call keeper.
int randomKeeperX = arc4random() % (int)self.post.contentSize.width;
int randomKeeperY = arc4random() % (int)self.post.contentSize.height;
CGPoint keeperLoc = ccp(randomKeeperX, randomKeeperY);
[self.post.keeper keeperLocation:keeperLoc];
}
#end
GoalPost.h
#import "GoalKeeper.h"
#interface GoalPost : CCNodeColor { }
#property GoalKeeper* keeper;
#end
GoalPost.m
#import "GoalPost.h"
#import "GoalKeeper.h"
#interface GoalPost ()
#property NSArray* post;
#end
#implementation GoalPost
- (instancetype)init
{
self = [super initWithColor:[CCColor lightGrayColor]];
if (self) {
....
[self addChild:aPost];
}
//Call goal keeper and add.
//for touch to run goal keeper.
[self setUserInteractionEnabled:true];
self.keeper = [GoalKeeper node];
//Put it in front of goal post.
self.keeper.position = ccp(self.contentSize.width/2, 0);
[self addChild:self.keeper];
}
return self;
}
-(void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
CGPoint touchLoc = [touch locationInNode:self];
NSLog(#"Goal touched: %f %f", touchLoc.x, touchLoc.y);
//Goal keeper call
[self.keeper keeperLocation:touchLoc];
}
#end
Finally, GoalKeeper.m
.h
#import "cocos2d.h"
#interface GoalKeeper : CCNodeColor { }
-(void)keeperLocation:(CGPoint)location;
#end
.m
#import "GoalKeeper.h"
#interface GoalKeeper()
#property CGPoint touchLoc;
#end
#implementation GoalKeeper
- (instancetype)init
{
self = [super initWithColor:[CCColor blueColor]];
if (self) {
[self setUserInteractionEnabled:TRUE];
[self setContentSize:CGSizeMake(30, 70)];
}
return self;
}
-(void)keeperLocation:(CGPoint)location {
CGPoint offset = ccpSub(location, self.position);
int targetX = self.position.x + offset.x;
int targetY = self.position.y + offset.y;
CGPoint targetPosition = ccp(targetX, targetY);
CCActionMoveBy *actionMoveBy = [CCActionMoveBy actionWithDuration:0.5f position:offset];
[self runAction:actionMoveBy];
}
#end

NSColorWell and Cocos2D Layer issue

I am tying to dynamically change my background color on the fly using an NSColorWell. I am using cocos2d framework for desktop.
I have a GLView that shows the cocos2d window and I have a NSColorwell off to the side. When I click on the color well color, I want the background to change to that color ‘on-the-fly’ so to speak.
I have all my variables in one class called settings. And my AppDeletgate has the IBActions for the NSColorWell. And the Cocos2D GLview lives in the HelloWorldLayer.
When ever I chose a color in the NSColorWell I get an exception thrown. Any idea why?
Forgive the code dump but I wanted you all to be able to see exactly what I was doing. I have tried many different methods of implementing this.
AppDelegate.h
#import "cocos2d.h"
#interface AnimatorAppDelegate : NSObject <NSApplicationDelegate>
{
NSWindow *window_;
MacGLView *glView_;
#property (assign) IBOutlet NSWindow *window;
#property (assign) IBOutlet MacGLView *glView;
- (IBAction)bgColorWell:(id)sender;
#end
AppDelegate.m
#import "AppDelegate.h"
#import "HelloWorldLayer.h"
#import "Settings.h"
#implementation AnimatorAppDelegate
#synthesize window=window_, glView=glView_;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
CCDirectorMac *director = (CCDirectorMac*) [CCDirector sharedDirector];
[director setDisplayFPS:YES];
[director setOpenGLView:glView_];
[director setResizeMode:kCCDirectorResize_AutoScale];
[window_ setAcceptsMouseMovedEvents:NO];
[director runWithScene:[HelloWorldLayer scene]];
}
#pragma mark AppDelegate - IBActions
- (IBAction)toggleFullScreen: (id)sender
{
CCDirectorMac *director = (CCDirectorMac*) [CCDirector sharedDirector];
[director setFullScreen: ! [director isFullScreen] ];
}
- (IBAction)bgColorWell:(id)sender {
NSLog(#"Color Well %#", [sender color]);
HelloWorldLayer *layer = [[HelloWorldLayer alloc] init];
[layer setBackgroundColorForLayer:[sender color]];
}
#end
Settings.h
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#interface Settings : NSObject {
NSColor *_backgroundColor;
}
+ (Settings *) sharedSettings;
- (NSColor *) returnBackgroundColor;
- (void) setBackgroundColor : (NSColor *)c;
#end
Settings.m
#import "Settings.h"
#implementation Settings
static Settings * _sharedSettings;
- (id) init {
if (self = [super init]){
//_backgroundColor = [NSColor colorWithDeviceRed:102/255.0f green:205/255.0f blue:170/255.0f alpha:1.0];
_backgroundColor = [NSColor blueColor];
}
return self;
}
+ (Settings *) sharedSettings {
if (!_sharedSettings) {
_sharedSettings = [[Settings alloc] init];
}
return _sharedSettings;
}
- (NSColor *) returnBackgroundColor {
return _backgroundColor;
}
- (void) setBackgroundColor : (NSColor *)c {
_backgroundColor = c;
}
#end
HelloWorldLayer.h
#import "cocos2d.h"
#import "Settings.h"
// HelloWorldLayer
#interface HelloWorldLayer : CCLayer
{
}
// returns a CCScene that contains the HelloWorldLayer as the only child
+(CCScene *) scene;
- (void) setBackgroundColorForLayer : (id) sender;
#end
HelloWorldLayer.m
// Import the interfaces
#import "AppDelegate.h"
#import "HelloWorldLayer.h"
#import "Settings.h"
// HelloWorldLayer implementation
#implementation HelloWorldLayer
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
HelloWorldLayer *layer = [HelloWorldLayer node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
// on "init" you need to initialize your instance
-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super" return value
if( (self=[super init])) {
CGSize winSize = [[CCDirector sharedDirector] winSize];
}
return self;
}
- (void) setBackgroundColorForLayer : (id) sender {
Settings *mySettings = [Settings sharedSettings];
float red = [mySettings returnBackgroundColor].redComponent * 255;
float green = [mySettings returnBackgroundColor].greenComponent * 255;
float blue = [mySettings returnBackgroundColor].blueComponent * 255;
CCLayerColor* colorLayer = [CCLayerColor layerWithColor:ccc4(red, green, blue, 255)];
[self addChild:colorLayer z:0];
NSLog(#"Red color: %f", red);
NSLog(#"Green color: %f", green);
NSLog(#"Blue color: %f", blue);
//[self setBackgroundColorForLayer:sender];
NSLog(#"This Background color is %#" , [mySettings returnBackgroundColor]);
}