Hide and Remove Admob banner - c++

I have developed a game in cocos2dx for iOS.Now i integrated banner view for show admob banner ad.
i write code for admob in appController class like that.
NSLog(#"ADMOB");
CGSize winSize = [[CCDirector sharedDirector]winSize];
bannerView_ = [[GADBannerView alloc]
initWithFrame:CGRectMake(size.width/2-160,
size.height -
GAD_SIZE_320x50.height,
GAD_SIZE_320x50.width,
GAD_SIZE_320x50.height)];
bannerView_.adUnitID =#"a*******";
bannerView_.delegate=self;
[viewController.view bannerView_];
bannerView_.rootViewController = viewController;
[bannerView_ loadRequest:[GADRequest request]];
GADRequest request = [[GADRequest alloc] init];
request.testing = [NSArray arrayWithObjects:
GAD_SIMULATOR_ID, nil];
[bannerView_ loadRequest:request];
Now I have to hide or remove admob banner ad from cpp class. So I have to call C++ class to objective. C++ call and then call to appcontroller class. and suppose to de remove banner view like that
[bannerView removeFromSuperview];
[bannerView setDelegate:nil];
[bannerView release];
bannerView = nil;
but banner view not removing from that call.
please help me to getting out from this issues.
any help will be appreciated.

Is your ad getting displayed properly,
since this line does nothing:-
[viewController.view bannerView_];
Shouldn't be this like
[viewController.view addSubview:bannerView_];

Related

Instantly remove AdMob banner?

I've made a game in using cocos2d and have integrated an admob banner in the appdelegate so that it is displayed on all view controllers. Ive implemented a remove ads feature which happens in the "game over" screen. Here is my problem. The remove ads function works but does not take affect immediately. I have to kill the app and restart it for the banner to go away. Is there a way to just reload the appdelegate and the ads are removed instantly instead of having to reload the app? PLEASE HELP This is driving me crazy.
EDITED: HERE IS MY CURRENT CODE. THE BANNER LOADS WHEN THE APP LOADS I AM TRYING TO MAKE THE BANNER GO AWAY WHEN THE USER PURCHASES TO REMOVE IT WITHOUT HAVING TO RESTART THE APP(CLOSE THE APP RESTART)
AppDelegate.h
#import "GADBannerViewDelegate.h"
// Added only for iOS 6 support
#interface MyNavigationController : UINavigationController <CCDirectorDelegate>
#end
#class RootViewController;
#class GADBannerView, GADRequest;
#interface AppController : NSObject <UIApplicationDelegate,ADBannerViewDelegate,UIActionSheetDelegate, GKLeaderboardViewControllerDelegate, GameCenterManagerDelegate,ChartboostDelegate,GADBannerViewDelegate>
{
UIWindow *window_;
MyNavigationController *navController_;
CCDirectorIOS *director_;
UIViewController *tempVC; // weak ref
RootViewController *viewController_;
//Admob
GADBannerView *bannerView_;
BOOL isAdPositionAtTop_;
}
AppDelegate.m i use calladmob to start ads if removeads is "no"
-(void)callAdMob{
//Admob ads
CGPoint origin = CGPointMake(0.0,self.window.frame.size.height -CGSizeFromGADAdSize(kGADAdSizeSmartBannerPortrait).height);
bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerPortrait origin:origin];
bannerView_.adUnitID = #"idhere";
bannerView_.delegate = self;
[bannerView_ setRootViewController:tempVC];
CGRect adBannerViewFrame = [bannerView_ frame];
int lSizeValue=0;
if([[CCDirector sharedDirector] winSize].width==320)
lSizeValue=50;
else if([[CCDirector sharedDirector] winSize].width==768)
lSizeValue=66;
adBannerViewFrame.origin.x = 0;
adBannerViewFrame.origin.y = [[CCDirector sharedDirector] winSize].height-lSizeValue;
[bannerView_ setFrame:adBannerViewFrame];
[[self navController].view addSubview:bannerView_];
[bannerView_ loadRequest:[self createRequest]];}
Why not send a notification from your "remove ads" function?
And then anywhere you have an AdMob banner, you can add an observer that watches for that notification and the method that gets called would do something like:
[bannerView removeFromSuperview] or bannerView.hidden = YES
provided you have your AdMob banner connected to an IBOutlet.

Create a left pointed UIBarButton in a UINavigation Bar Programmatically

I'm trying to create an app with a settings page just like IOS's native settings app. However I cannot find out how to create a UIBarButton that is left pointed. Basically I want a left pointed button on the top of a navigation bar, that you can set to call a function. I am using the engine cocos2d, if this helps. I am creating the entire app programmatically.
Here is my code for creating the UINavigationBar...
UINavigationBar *naviBarObj = [[[UINavigationBar alloc] initWithFrame:CGRectMake(0, 20, 320, 44)] autorelease];
[[[CCDirector sharedDirector] openGLView] addSubview:naviBarObj];
[naviBarObj release];
UINavigationItem *item = [[[UINavigationItem alloc] initWithTitle:#"Title"] autorelease];
UIBarButtonItem *back = [[[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(back)] autorelease];
item.leftBarButtonItem = back;
[naviBarObj setItems:[NSArray arrayWithObjects:item, nil] animated:YES];
Although this creates a square back button.
My goal is to create something like this
I am aware that a UINavigationItem
UINavigationItem *back2 = [[[UINavigationItem alloc] initWithTitle:#"Settings"] autorelease];
with [naviBarObj setItems:[NSArray arrayWithObjects:back,item, nil] animated:YES];
will create that effect, but unfortunately I am not currently using a UINavigationController class. This is all made in a CCLayer.
Thank You!!
One way would be to use a custom back image, but that would be kind of tedious.
Another way would be like exactly what you mentioned in your last part, to create another "dummy" previous navigation item with the title set to the text you want to be on your back button, and then adding both items to the UINavigationBar.
UINavigationItem *item = [[[UINavigationItem alloc] initWithTitle:#"Title"] autorelease];
UINavigationItem *back2 = [[[UINavigationItem alloc] initWithTitle:#"Settings"] autorelease];
[naviBarObj setItems:[NSArray arrayWithObjects:back2,item, nil] animated:YES];
However I don't think you will need a UINavigationController for these to happen. In order to handle your back button event, I suppose you can try making your CCLayer a delegate of UINavigationBar, ie. UINavigationBarDelegate:
#interface MyLayer : CCLayer <UINavigationBarDelegate> {
}
then setting the delegate of your UINavigationBar to that layer:
naviBarObj.delegate = self;
and finally implement didPopItem to get notified of the event when the button is clicked:
- (void) navigationBar:(UINavigationBar *)navigationBar didPopItem:(UINavigationItem *)item
{
[self back]; // or whatever
}
Let me know if this works.

UINavigationBar Not Visible With UITabBarController [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Trying to construct a tableview with a navigation bar at top
I have got a UITabBarController with a TabBar in it. Now what I need is a UINavigationBar on top of controller just to show title and a button. For that, I have taken a stand alone UINavigationBar, but it is not visible I can't understand why.
I am sorry but haven't found any appropriate solution up till now. Is there any specific way to use and access stand alone UINavigationBar with UITabBarController?
Create your UIViewControllers and there views using Interface Builder or coding to be displayed in each of the Tabs. Add the NavigationBar to each one of these views.
Then add these UIViewControllers to the UITabViewController's viewControllers NSArray.
ie. In you AppDelegate's application didFinishLaunching function
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
Add the UINavigationBar to the viewController1 and viewControllers2 views, either using IB or coding it.
Following link worked for me perfectly!
Trying to construct a tableview with a navigation bar at top

Cocos2d Admob Integration - Click on the banner doesn't show full view

I am trying to integrate AdMob into my cocos2d Game and I having some problems. Basically the ad shows up but when I click on it, the banner disappears and the full view doesn't show up. I am using a slight modified version of the code found on google admob page. Here is my code:
-(void) addAdMobBanner{
NSLog(#"adding Admob");
controller = [[RootViewController alloc]init];
CGSize size = [[CCDirector sharedDirector] winSize];
controller.view.frame = CGRectMake(0,0,size.width,size.height);
// Create a view of the standard size at the bottom of the screen.
bannerView = [[GADBannerView alloc]
initWithFrame:CGRectMake(size.width/2+50,
size.height-GAD_SIZE_468x60.height,
GAD_SIZE_468x60.width,
GAD_SIZE_468x60.height)];
// Specify the ad's "unit identifier." This is your AdMob Publisher ID.
bannerView.adUnitID = #"xxxxxxxxxxxx";
// Let the runtime know which UIViewController to restore after taking
// the user wherever the ad goes and add it to the view hierarchy.
bannerView.rootViewController = controller;
[controller.view addSubview:bannerView];
[[[CCDirector sharedDirector] openGLView]addSubview : controller.view];
[bannerView loadRequest:[GADRequest request]];
}
Thanks guys
It doesn't work on a simulator but it works on the device, but if you are using in the landscape mode you have to transform it yourselves.

Game Center leaderboards won't show up

Okay, I've been trying to display the Game Center leaderboards in my Cocos2d iPhone game.
I have progressed and I got this piece of code:
- (void) showLeaderboard {
tempVC=[[UIViewController alloc] init];
GKLeaderboardViewController *leaderboardController = [[GKLeaderboardViewController alloc] init];
if (leaderboardController != nil)
{
leaderboardController.leaderboardDelegate = self;
[[[CCDirector sharedDirector] openGLView] addSubview:tempVC.view];
[tempVC presentModalViewController:leaderboardController animated: YES];
}
}
When I run that in the simulator, I see it becomes portrait mode, so I know something happened. BUT nothing appears. No leaderboards come out. What is wrong?
My application is already linked with the iTunes thingy.
I am authenticated at the beginning of the game sucessfully.
I already submitted scores (with a nice thing called GameKitHelper)
Go to the AppDelegate.m and change:
[window addSubview: viewController.view];
to
window.rootViewController = viewController;
then call it
GameKitHelper* gkHelper = [GameKitHelper sharedGameKitHelper];
[gkHelper showLeaderboard];
This works on cocos2d 1.0.0 RC1