SKScene being called twice when I add an iAd banner to ViewController - ios

I found out that my Main menu Scene is being called twice when I have a iad banner in my viewcontroller. Does anyone know why it is acting this way?
-(void)bannerViewDidLoadAd:(ADBannerView *)banner{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[banner setAlpha:1];
[UIView commitAnimations];
}
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[banner setAlpha:0];
[UIView commitAnimations];
}
in viewcontroller.h
#interface GameViewController : UIViewController <ADBannerViewDelegate>{
I think they may be a problem with the UIView because I heard UIViews don't work well with Sprite-Kit

I am not sure may be this work.
ViewController.m
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
[banner setAlpha:0];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(handleNotification:) name:#"hideAd" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(handleNotification:) name:#"showAd" object:nil];
}
- (void)handleNotification:(NSNotification *)notification
{
if ([notification.name isEqualToString:#"hideAd"]) {
[banner setAlpha:0];
}else if ([notification.name isEqualToString:#"showAd"]) {
[banner setAlpha:1];
}
}
Call with this in your scene where you want
[[NSNotificationCenter defaultCenter] postNotificationName:#"showAd" object:nil];

Related

How can I add a smooth animation to my View when shifting it up/down?

I'm using the following code to shift my UIView up/down when the keyboard is initiated in order to keep it from covering my textfields. The code works great, but the movement isn't as seamless as I'd like it to be. Would love some help re: the code I'd need to add in order to make the view movement smooth rather than choppy. Thanks in advance :)
Update: I changed KeyboardDidShow to KeyboardWillShow, and doing so causes the following crash error.
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[ViewController
keyboardDidShow:]: unrecognized selector sent to instance 0x145e0f530
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
[self.view endEditing:YES];
return YES;
}
- (void)keyboardWillShow:(NSNotification *)notification
{
// Assign new frame to your view
[self.view setFrame:CGRectMake(0,-110,375,667)]; //here taken -20 for example i.e. your view will be scrolled to -20. change its value according to your requirement.
}
-(void)keyboardDidHide:(NSNotification *)notification
{
[self.view setFrame:CGRectMake(0,0,375,667)];
}
Try wrapping your frame changes in [UIView animateWithDuration:animations:]
[UIView animateWithDuration:.25 animations:^{
[self.view setFrame:CGRectMake(0,0,375,667)];
}];
Simply add your code inside animation blocks:
- (void)keyboardDidShow:(NSNotification *)notification{
[UIView animateKeyframesWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionAllowUserInteraction
animations:^{
[self.view setFrame:CGRectMake(0,-110,375,667)];
} completion:nil];
}
-(void)keyboardDidHide:(NSNotification *)notification{
[UIView animateKeyframesWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionAllowUserInteraction
animations:^{
[self.view setFrame:CGRectMake(0,0,375,667)];
} completion:nil];
}

Error hidding iAdBanner when fails and trying to show my own button Xcode iOS7

Hi I'm trying to hide the iAdBanner when it fails and I want to show my own button with my other URL itunes app, but it doesn't work on iOS 7. Can someone help me? thank you!
(The iAd banner works properly).
My code is:
- (void)viewDidLoad {
[super viewDidLoad];
_myBanner.hidden = TRUE;
_myBanner.enabled = FALSE;
}
-(void)bannerViewDidLoadAd:(ADBannerView *)banner {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[banner setAlpha:1];
[UIView commitAnimations];
}
- (void)bannerView:(ADBannerView *) banner didFailToReceiveAdWithError:(NSError *)error
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[banner setAlpha:0];
[UIView commitAnimations];
if (_adBannerViewIsVisible) {
_adBannerViewIsVisible = NO;
[self fixupAdView:[UIDevice currentDevice].orientation]; /*This doesn't work*/
}
_myBanner.hidden = FALSE;
_myBanner.enabled = TRUE;
[ADBannerView removeFromSuperview]; /*This doesn't work*/
[ADBannerView release]; /*This doesn't work*/
}
- (IBAction)tapBanner:(id)sender
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#""]];
}
- (void)bannerView:(ADBannerView *) banner didFailToReceiveAdWithError:(NSError *)error
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[banner setAlpha:0];
[UIView commitAnimations]; _myBanner.hidden = YES;
_myBanner.enabled = NO;
[_myBanner removeFromSuperview];
[_myBanner release];
}

Hide iAd bannerview in spritekit scene

I am trying to add iads to my new sprite kit game. The problem is that i do not need the ad to be on all the scenes. I've started to create an ADBannerView in the mainstoryboard. After that i'm trying to use NSNotification to hide and show the ads in different scenes, but its not working. the ad is still showing even though i've added into Menu.m(scene):
[[NSNotificationCenter defaultCenter] postNotificationName:#"hideAd" object:nil];
ViewController.m
-(void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
// Configure the view.
SKView * skView = (SKView *)self.view;
//skView.showsFPS = YES;
//skView.showsNodeCount = YES;
//skView.showsPhysics = YES;
if (!skView.scene) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(handleNotification:) name:#"hideAd" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(handleNotification:) name:#"showAd" object:nil];
SKScene * scene = [Menu sceneWithSize:skView.bounds.size];
NSLog(#"%#", scene);
// Present the scene.
[skView presentScene:scene];
}
}
-(void)bannerViewDidLoadAd:(ADBannerView *)banner {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[banner setAlpha:1];
[UIView commitAnimations];
}
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[banner setAlpha:0];
[UIView commitAnimations];
}
- (void)handleNotification:(NSNotification *)notification
{
if ([notification.name isEqualToString:#"hideAd"]) {
[self hidesBanner];
}else if ([notification.name isEqualToString:#"showAd"]) {
[self showsBanner];
}
}
-(void)hidesBanner {
NSLog(#"HIDING BANNER");
[adView setAlpha:0];
}
-(void)showsBanner {
NSLog(#"SHOWING BANNER");
[adView setAlpha:1];
}
It is not good to create an iAd if you do not intend to show it.
According to Apple's Banner View Best Practices:
• Only create a banner view when you intend to display it to the user. Otherwise, it may cycle through ads and deplete the list of available advertising for your app.
• If the user navigates from a screen of content with a banner view to a screen that does not have a banner view, and you expect them to be on that screen for a long period of time, remove the banner view from the view hierarchy, set its delegate to nil and release it before transitioning to the new screen of content. More generally, avoid keeping a banner view around when it is invisible to the user.
I remember reading about a hidden property a while back but looking at the iAd Framework Reference, I cannot find any such property. I recommend you follow Apple's guidelines if you do not want to display an ad in a specific scene.

iAd appears before it is loaded in iOS using subviews

Hello I have an issue where my ads appear before they are even loaded.
I got this
#pragma mark iAd Delegate Methods
- (AppDelegate *) appdelegate {
return (AppDelegate *)[[UIApplication sharedApplication] delegate];
}
-(void) viewWillAppear:(BOOL)animated{
//this part set alpha 0 does nothing
[_UIiAD setAlpha:0];
_UIiAD = [[self appdelegate] UIiAD];
_UIiAD.delegate = self;
[_UIiAD setFrame:CGRectMake(0,470,320,50)];
[self.view addSubview:_UIiAD];
}
-(void)bannerViewDidLoadAd:(ADBannerView *)banner{
NSLog(#"ads loaded");
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[_UIiAD setAlpha:1];
[UIView commitAnimations];
}
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error{
NSLog(#"ads not loaded");
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[_UIiAD setAlpha:0];
[UIView commitAnimations];
}
I have tried to stick
[_UIiAD setAlpha:0];
In various parts of my code, but still same issue.
If your AdBannerView has been initialized outside of this snippet (presumably in AppDelegate from what you show in your viewWillAppear: code), it can still load ads even before it has a delegate.
Instead of programmatically adding the iAd banner as a subview after creating it somewhere else, have you tried adding it using Interface Builder and just using the code to hide/unhide based on whether or not it's received content.

IOS : View height is getting reduced when performing animation

I have the following code for moving the view up and down when keyboard appears on a text field.
-(void)textFieldDidBeginEditing:(UITextField *)textField{
NSLog(#"height before animation%f",self.view.frame.size.height);
NSLog(#"%f",self.view.frame.size.height);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
[self.view setFrame:CGRectMake(0,-216,320,460)];
[UIView commitAnimations];
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
[self.view setFrame:CGRectMake(0,0,320,460)];
[UIView commitAnimations];
NSLog(#"height after animation %f",self.view.frame.size.height);
}
Here's a sample log I get when keyboard appears and then editing is finished :
2014-01-21 11:00:51.194 Master-view[456:70b] height before animation 568.000000
2014-01-21 11:00:53.635 Master-view[456:70b] height after animation 460.000000
The height of the view seems to get reduced which is making bottom part of the screen non-interactable. Why is this happening ?
View is moving up and is coming down too without issue. Also visually there appears to be no difference. All elements which were there before moving up are there after coming down. But the elements at bottom of the screen ( beyond the height of 460.0) are not intractable.
-(void)textFieldDidBeginEditing:(UITextField *)textField{
NSLog(#"height before animation%f",self.view.frame.size.height);
NSLog(#"%f",self.view.frame.size.height);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
[self.view setFrame:CGRectMake(0,-216,320,self.view.frame.size.height)];
[UIView commitAnimations];
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
[self.view setFrame:CGRectMake(0,0,320,self.view.frame.size.height)];
[UIView commitAnimations];
NSLog(#"height after animation %f",self.view.frame.size.height);
}
I set the frame as per view height you only change the y position when key beginediting and try to set it back when end editing
may this help you
Try changing like
-(void)textFieldDidBeginEditing:(UITextField *)textField{
NSLog(#"height before animation%f",self.view.frame.size.height);
NSLog(#"%f",self.view.frame.size.height);
CGRect viewFrame = self.view.frame;
viewFrame.origin.y = -216;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
CGRect viewFrame = self.view.frame;
viewFrame.origin.y = 0;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
NSLog(#"height after animation %f",self.view.frame.size.height);
}
We use NSNotification keyboard will show and keyboard will hide for that.
we use add notification for keyboard show and hide we add in viewWillAppear a notification and remove Notification in viewWillDisappear.
-(void)viewWillAppear:(BOOL)animated{**
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillHide)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)viewWillDisappear:(BOOL)animated**
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
-(void)keyboardWillShow {
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
else if (self.view.frame.origin.y < 0)
{
[self setViewMovedUp:NO];
}
}
-(void)keyboardWillHide {
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
else if (self.view.frame.origin.y < 0)
{
[self setViewMovedUp:NO];
}
}
-(void)setViewMovedUp:(BOOL)movedUp
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
CGRect rect = self.view.frame;
if (movedUp)
{
rect.origin.y -= kOFFSET_FOR_KEYBOARD;
rect.size.height += kOFFSET_FOR_KEYBOARD;
}
else
{
rect.origin.y += kOFFSET_FOR_KEYBOARD;
rect.size.height -= kOFFSET_FOR_KEYBOARD;
}
self.view.frame = rect;
[UIView commitAnimations];
}

Resources