iOS 7 iAd cocos2d deprecated - objective-c

Im trying to get iAd implemented to a iOS 7 game I made with cocos2d, I added iad framework in xcode and have the setup below
in #implementation I have set
ADBannerView *_bannerView;
then
-(id)init
{
if( (self= [super init]) )
{
// On iOS 6 ADBannerView introduces a new initializer, use it when available.
if ([ADBannerView instancesRespondToSelector:#selector(initWithAdType:)]) {
ADBannerView *_adView = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];
} else {
_adView = [[ADBannerView alloc] init];
}
_adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifierPortrait];
_adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
[[[CCDirector sharedDirector]view]addSubview:_adView];
[_adView setBackgroundColor:[UIColor clearColor]];
[[[CCDirector sharedDirector]view]addSubview:_adView];
_adView.delegate = self;
}
[self layoutAnimated:YES];
return self;
}
- (void)layoutAnimated:(BOOL)animated
{
// As of iOS 6.0, the banner will automatically resize itself based on its width.
// To support iOS 5.0 however, we continue to set the currentContentSizeIdentifier appropriately.
CGRect contentFrame = [CCDirector sharedDirector].view.bounds;
if (contentFrame.size.width < contentFrame.size.height) {
//_bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
[adView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
} else {
_bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape;
}
CGRect bannerFrame = _bannerView.frame;
if (_bannerView.bannerLoaded) {
contentFrame.size.height -= _bannerView.frame.size.height;
bannerFrame.origin.y = contentFrame.size.height;
} else {
bannerFrame.origin.y = contentFrame.size.height;
}
[UIView animateWithDuration:animated ? 0.25 : 0.0 animations:^{
_bannerView.frame = bannerFrame;
}];
}
- (void)bannerViewDidLoadAd:(ADBannerView *)banner {
[self layoutAnimated:YES];
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
[self layoutAnimated:YES];
}
I get an error about '_adview' and as well as 'currentContentSizeIdentifier' deprecated, can anyone help me get this working correctly? Ive checked online all day and couldnt get any of the code working.
Thanks in advance!

Try this New Code:
- (void)createAdBannerView
{
_adBannerView = [[ADBannerView alloc] initWithFrame:CGRectZero];
_adBannerView.delegate = self;
[_adBannerView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
CGRect frame = _adBannerView.frame;
frame.origin.y = -frame.size.height;
frame.origin.x = 0.0f;
_adBannerView.frame = frame;
AppDelegate * app = (((AppDelegate*) [UIApplication sharedApplication].delegate));
[app.navController.view addSubview:_adBannerView];
}
Here new iAd Code for Cocos2d 3.0.

Related

iAdSuite bug leaves white space when ad disappears

I'm trying to incorporate the iAdSuite tab bar view implementation in my app and I'm seeing the same problem in the suite and my app. When the ad appears, my content's view is getting properly resized and the ad appears correctly. When the ad then disappears, it leaves behind white space where it was located. However, I've confirmed that my content view does get resized back to its original height and it gets drawn down to its original bounds. You just can't see the part where the ad was. I've made sure every view gets a layoutIfNeeded and just about everything else I can think of to no avail. Any thoughts?
Edit: I've figured out what the problem is. Apple's example apparently adds _bannerView to self.view every time showBannerView: is called but never removes the view. That still doesn't make complete sense since the banner view is being moved offscreen, but removing it does solve the white space problem. My solution is as follows, but if anyone has a more elegant way, let me know.
- (void)layoutAnimated:(BOOL)animated {
if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) {
_bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
} else {
_bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape;
}
CGRect contentFrame = self.view.bounds;
contentFrame.origin = CGPointMake(0.0, 0.0);
CGRect bannerFrame = _bannerView.frame;
if (_bannerView.bannerLoaded) {
contentFrame.size.height -= _bannerView.frame.size.height;
bannerFrame.origin.y = contentFrame.size.height;
} else {
bannerFrame.origin.y = contentFrame.size.height;
}
[UIView animateWithDuration:animated ? 0.25 : 0.0 animations:^{
_contentView.frame = contentFrame;
[_contentView layoutIfNeeded];
_bannerView.frame = bannerFrame;
}
completion:^(BOOL finished) {
if (!_bannerView.bannerLoaded) {
[_bannerView removeFromSuperview];
_bannerView=nil;
}
}];
}
- (void)showBannerView:(ADBannerView *)bannerView animated:(BOOL)animated
{
_bannerView = bannerView;
if (![self.view.subviews containsObject:_bannerView])
[self.view addSubview:_bannerView];
[self layoutAnimated:animated];
}
- (void)hideBannerView:(ADBannerView *)bannerView animated:(BOOL)animated
{
[self layoutAnimated:animated];
}
I had the same problem. Removing the bannerview from the super view in the hideBannerView delegate method seems to have solved it.
- (void)hideBannerView:(ADBannerView *)bannerView animated:(BOOL)animated
{
[self layoutAnimated:animated];
[_bannerView removeFromSuperview];
_bannerView = nil;
}
Thanks for this question and answer, I was pulling hairs with this one.
I changed the damn code like this and now the hiding animation works. I wonder why apple publishes buggy sample code...
- (void)layoutAnimated:(BOOL)animated hide:(BOOL)hide
{
if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) {
_bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
} else {
_bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape;
}
CGRect contentFrame = self.view.bounds;
CGRect bannerFrame = _bannerView.frame;
if (!hide) {
contentFrame.size.height -= _bannerView.frame.size.height;
bannerFrame.origin.y = contentFrame.size.height;
} else {
contentFrame.size.height += _bannerView.frame.size.height;
bannerFrame.origin.y = contentFrame.size.height;
}
[UIView animateWithDuration:animated ? 0.25 : 0.0 animations:^{
_contentView.frame = contentFrame;
[_contentView layoutIfNeeded];
_bannerView.frame = bannerFrame;
} completion:^(BOOL finished) {
if (hide) {
[_bannerView removeFromSuperview];
_bannerView=nil;
}
}];
}
- (void)showBannerView:(ADBannerView *)bannerView animated:(BOOL)animated
{
_bannerView = bannerView;
[self.view addSubview:_bannerView];
[self layoutAnimated:animated hide:NO];
}
- (void)hideBannerView:(ADBannerView *)bannerView animated:(BOOL)animated
{
[self layoutAnimated:animated hide:YES];
}

Admob banner not getting remove from superview

I am developing one 2d game using cocos2d framework, in this game i am using admob for advertising, in some classes not in all classes but admob banner is visible in every class and after some time game getting crash also.
I am not getting how admob banner is comes in every class in fact i have not declare in Rootviewcontroller class. can any one suggest me how to integrate Admob in cocos2d game, i want Admob banner in particular classes not in every class,
I am using latest google admob sdk, my code is below:
Thanks in advance
`
-(void)AdMob{
NSLog(#"ADMOB");
CGSize winSize = [[CCDirector sharedDirector]winSize];
// Create a view of the standard size at the bottom of the screen.
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
bannerView_ = [[GADBannerView alloc]
initWithFrame:CGRectMake(size.width/2-364,
size.height -
GAD_SIZE_728x90.height,
GAD_SIZE_728x90.width,
GAD_SIZE_728x90.height)];
}
else { // It's an iPhone
bannerView_ = [[GADBannerView alloc]
initWithFrame:CGRectMake(size.width/2-160,
size.height -
GAD_SIZE_320x50.height,
GAD_SIZE_320x50.width,
GAD_SIZE_320x50.height)];
}
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
bannerView_.adUnitID =#"a15062384653c9e";
}
else {
bannerView_.adUnitID =#"a15062392a0aa0a";
}
bannerView_.rootViewController = self;
[[[CCDirector sharedDirector]openGLView]addSubview:bannerView_];
[bannerView_ loadRequest:[GADRequest request]];
GADRequest *request = [[GADRequest alloc] init];
request.testing = [NSArray arrayWithObjects:
GAD_SIMULATOR_ID, nil]; // Simulator
[bannerView_ loadRequest:request];
}
//best practice for removing the barnnerView_
-(void)removeSubviews{
NSArray* subviews = [[CCDirector sharedDirector]openGLView].subviews;
for (id SUB in subviews){
[(UIView*)SUB removeFromSuperview];
[SUB release];
}
NSLog(#"remove from view");
}
//this makes the refreshTimer count
-(void)targetMethod:(NSTimer *)theTimer{
//INCREASE OF THE TIMER AND SECONDS
elapsedTime++;
seconds++;
//INCREASE OF THE MINUTOS EACH 60 SECONDS
if (seconds>=60) {
seconds=0; minutes++;
[self removeSubviews];
[self AdMob];
}
NSLog(#"TIME: %02d:%02d", minutes, seconds);
}
`
UPDATES: Refer generalised answer here: Admob-banner-integration-in-cocos2d
I hope already u got solution. If not then here is all code for Admob integration in cocos2D game.
#define ENABLE_ADMOB 1
//#define COCOS2D_2_0 1
#interface MyMainMenu : CCLayer
{
#ifdef ENABLE_ADMOB
GADBannerView *mBannerView;
#endif
}
#implementation MyMainMenu
-(void)onEnter
{
[super onEnter];
#ifdef ENABLE_ADMOB
#ifdef COCOS2D_2_0
AppController *app = (AppController*)[[UIApplication sharedApplication] delegate];
#else
AppDelegate* app = (AppDelegate*)[[UIApplication sharedApplication] delegate];
#endif
mBannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
// Specify the ad's "unit identifier." This is your AdMob Publisher ID.
mBannerView.adUnitID = MY_BANNER_UNIT_ID;
// Let the runtime know which UIViewController to restore after taking
// the user wherever the ad goes and add it to the view hierarchy.
//size
#ifdef COCOS2D_2_0
mBannerView.rootViewController = app.navController;
[app.navController.view addSubview:mBannerView];
#else
mBannerView.rootViewController = app.viewController;
[app.viewController.view addSubview:mBannerView];
#endif
// Initiate a generic request to load it with an ad.
[mBannerView loadRequest:[GADRequest request]];
CGSize AdSize = kGADAdSizeBanner.size;
CGRect frame = mBannerView.frame;
frame.origin.y = -50.0f;
#ifdef COCOS2D_2_0
frame.origin.x = (app.navController.view.bounds.size.width - AdSize.width) / 2 ;
#else
frame.origin.x = (app.viewController.view.bounds.size.width - AdSize.width) / 2 ;
#endif
mBannerView.frame = frame;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
frame = mBannerView.frame;
frame.origin.y = 0.0f;
mBannerView.frame = frame;
[UIView commitAnimations];
#endif
}
-(void)showBannerView
{
if (mBannerView)
{
[UIView animateWithDuration:0.5
delay:0.1
options: UIViewAnimationCurveEaseOut
animations:^
{
CGRect frame = mBannerView.frame;
frame.origin.y = 0.0f;
mBannerView.frame = frame;
}
completion:^(BOOL finished)
{
}];
}
}
-(void)hideBannerView
{
if (mBannerView)
{
[UIView animateWithDuration:0.5
delay:0.1
options: UIViewAnimationCurveEaseOut
animations:^
{
CGRect frame = mBannerView.frame;
frame.origin.y = -50.0f;
mBannerView.frame = frame;
}
completion:^(BOOL finished)
{
}];
}
}
-(void)dismissAdView
{
#ifdef ENABLE_ADMOB
if (mBannerView)
{
[UIView animateWithDuration:0.5
delay:0.1
options: UIViewAnimationCurveEaseOut
animations:^
{
CGRect frame = mBannerView.frame;
frame.origin.y = -50.0f;
mBannerView.frame = frame;
}
completion:^(BOOL finished)
{
[mBannerView setDelegate:nil];
[mBannerView removeFromSuperview];
mBannerView = nil;
}];
}
#endif
}
Full fix (tested with iOS 8.1 and Admob 6.12.0)
-(void)RemoveAds
{
if (adBanner != nil)
{
[adBanner setRootViewController:nil];
[adBanner removeFromSuperview];
adBanner = nil;
}
}
Since in cocos2d, you will have different classes for scenes.
My suggestion would be to create a separate class for the add banner and have a static method do the job for you. You will have to save a reference of the add banner in that class and by using the static methods you can add/remove it to/from the openglview.
For removing you will only do: [bannerView removeFromSuperview];

How to display imagepickercontroller through popoverview controller?

I have used the following code to get photos from album in iphone but it displays the error "UIImagePickerController must be presented via UIPopoverController"
- (void)showImagePicker:(UIImagePickerControllerSourceType)sourceType
{
if ([UIImagePickerController isSourceTypeAvailable:sourceType])
{
[self setupImagePicker:sourceType];
[self presentModalViewController:imagePickerController animated:YES];
}
}
- (void)setupImagePicker:(UIImagePickerControllerSourceType)sourceType
{
imagePickerController.sourceType = sourceType;
if (sourceType == UIImagePickerControllerSourceTypeCamera)
{
// user wants to use the camera interface
//
imagePickerController.showsCameraControls = NO;
if (imagePickerController.cameraOverlayView != self.view)
{
// setup our custom overlay view for the camera
//
// ensure that our custom view's frame fits within the parent frame
CGRect overlayViewFrame = imagePickerController.cameraOverlayView.frame;
CGRect newFrame = CGRectMake(0.0,
CGRectGetHeight(overlayViewFrame) -
self.view.frame.size.height - 9.0,
CGRectGetWidth(overlayViewFrame),
self.view.frame.size.height + 9.0);
self.view.frame = newFrame;
imagePickerController.cameraOverlayView = self.view;
}
}
}
-(IBAction)getPhoto:(id)sender {
imagePickerController = [[UIImagePickerController alloc] init];
[self showImagePicker:UIImagePickerControllerSourceTypePhotoLibrary];
}
Can any one please alter my code to work on iPad.
Thanks in advance.
-(IBAction)Click_event
{
UIImagePickerController *imagePickerController_=[[UIImagePickerController alloc] init];
UIPopoverController *popover_=[[UIPopoverController alloc] initWithContentViewController:imagePickerController_];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
imagePickerController_.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[popover_ presentPopoverFromRect:CGRectMake(400, 400, 0, 0) inView:self.Mybutton
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}

iAd issue:--> Every time empty iAd banners is coming instead of AdMobView

#pragma mark - iAd method.
/*Starts:iAd*/
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
Class cls = NSClassFromString(#"ADBannerView");
if(cls!=nil)
{
[self layoutForCurrentOrientation:YES];
}
}
-(void)createADBannerView
{
NSString *contentSize = UIInterfaceOrientationIsPortrait(self.interfaceOrientation) ? ADBannerContentSizeIdentifierPortrait : ADBannerContentSizeIdentifierPortrait;
CGRect frame;
frame.size = [ADBannerView sizeFromBannerContentSizeIdentifier:contentSize];
frame.origin = CGPointMake(0.0, CGRectGetMaxY(self.view.bounds));
// Now to create and configure the banner view
ADBannerView *bannerView = [[ADBannerView alloc] initWithFrame:frame];
// Set the delegate to self, so that we are notified of ad responses.
bannerView.delegate = self;
// Set the autoresizing mask so that the banner is pinned to the bottom
bannerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin;
bannerView.requiredContentSizeIdentifiers = [NSSet setWithObjects:ADBannerContentSizeIdentifierPortrait, ADBannerContentSizeIdentifierPortrait, nil];
// At this point the ad banner is now be visible and looking for an ad.
[self.view addSubview:bannerView];
self.banner = bannerView;
[bannerView release];
}
-(void)layoutForCurrentOrientation:(BOOL)animated
{
CGFloat animationDuration = animated ? 0.2 : 0.0;
// by default content consumes the entire view area
CGRect contentFrame = self.view.bounds;
CGPoint bannerOrigin = CGPointMake(CGRectGetMinX(contentFrame), CGRectGetMaxY(contentFrame));
CGFloat bannerHeight = 0.0;
banner.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
bannerHeight = 55.0;
if(banner.bannerLoaded)
{
contentFrame.size.height -= bannerHeight;
bannerOrigin.y -= bannerHeight;
}
else
{
bannerOrigin.y += bannerHeight;
}
// And finally animate the changes, running layout for the content view if required.
NSLog(#"[UIApplication sharedApplication].statusBarOrientation = %d",[UIApplication sharedApplication].statusBarOrientation);
[UIView animateWithDuration:animationDuration
animations:^{
contentView.frame = contentFrame;
[contentView layoutIfNeeded];
banner.frame = CGRectMake(0,361, banner.frame.size.width, banner.frame.size.height);
}];
}
/*Ends:iAd*/
/*Starts:Added By:CP.Date:26-Dec-2010.
AdMob methods*/
#pragma mark - AdMobDelegate methods
-(NSString *) publisherIdForAd:(AdMobView *)adView
{
return #"a14f91029b8c719"; // this should be prefilled; if not, get it from www.admob.com
}
-(UIViewController *) currentViewControllerForAd:(AdMobView *)adView
{
return self;
}
-(UIColor *)adBackgroundColorForAd:(AdMobView *)adView
{
return [UIColor colorWithRed:0 green:0 blue:0 alpha:1]; // this should be prefilled; if not, provide a UIColor
}
-(UIColor *)primaryTextColorForAd:(AdMobView *)adView
{
return [UIColor colorWithRed:1 green:1 blue:1 alpha:1]; // this should be prefilled; if not, provide a UIColor
}
-(UIColor *)secondaryTextColorForAd:(AdMobView *)adView
{
return [UIColor colorWithRed:1 green:1 blue:1 alpha:1]; // this should be prefilled; if not, provide a UIColor
}
-(void) didReceiveAd:(AdMobView *)adView
{
adMobAd.frame = CGRectMake(0,361,320,55);
//adMobAd = [AdMobView requestAdOfSize:ADMOB_SIZE_748x110 withDelegate:self];
[self.view addSubview:adMobAd];
}
// Sent when an ad request failed to load an ad
-(void)didFailToReceiveAd:(AdMobView *)adView
{
Class cls = NSClassFromString(#"ADBannerView");
if(cls!=nil)
{
if(banner == nil)
{
[self createADBannerView];
}
[self layoutForCurrentOrientation:NO];
}
[adMobAd removeFromSuperview]; // Not necessary since never added to a view, but doesn't hurt and is good practice
[adMobAd release];
adMobAd = nil;
// we could start a new ad request here, but in the interests of the user's battery life, let's not
}
/*Ends:Added By:CP.Date:26-Dec-2010.
AdMob methods*/
first of all be clear about iad or admob , that are two different plateform for displaing adv in your app.
i think you are try to integrate admob adv. you need to following steps for it.
http://www.edumobile.org/iphone/iphone-programming-tutorials/how-to-admob-integrate-in-your-application-in-iphone/

iAd doesn't fit width when starts from landscape mode

I am switching and scaling properly iAd on portraid and landscape mode excepts when view starts from landscape mode. In this case, iAd remains at narrow width that corresponds to portraid. Then when rotate the device to portraid and back to landscape, is solved. How to solve it? Thank you.
- (void)viewDidLoad
{
//iAd
adView =[[ADBannerView alloc] initWithFrame:CGRectZero];
adView.requiredContentSizeIdentifiers = [NSSet setWithObjects: ADBannerContentSizeIdentifierPortrait, ADBannerContentSizeIdentifierLandscape, nil];
adView.delegate = self;
[self.view addSubview:adView];
[super viewDidLoad];
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration {
if (UIInterfaceOrientationIsPortrait(orientation)) {
adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
} else {
adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape;
}
}
You do not tell the adView with which orientation to start, so it starts with the default orientation.
Try adding the following in viewDidLoad:
if (UIInterfaceOrientationIsPortrait([UIDevice currentDevice].orientation)) {
adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
} else {
adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape;
}
[self.view addSubview:adView];