Hide iAd bannerview in spritekit scene - ios

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.

Related

iAd across view controllers (Singleton): Somehow not working

I've been struggling with this now for a long time. Where is my mistake? At the moment the hide and show seems to work, but everytime I come "back" to my viewcontroller I see that my view is shifted up, but there is no ad in there. But the first time I see the view controller, there is an ad. What am I doing wrong?
I just want to show the same ad across view controllers and this is like the parent UIViewController class, a lot of other view controllers inherit from:
#pragma mark View lifecycle
-(void)viewDidLoad
{
[super viewDidLoad];
if(![[NSUserDefaults standardUserDefaults] objectForKey:kInAppPurchaseNoAds]){
self.bannerContainer = ((AppDelegate *)[[UIApplication sharedApplication] delegate]).bannerView;
self.bannerContainer.frame = CGRectOffset(self.bannerContainer.frame, 0, self.view.frame.size.height);
[self.view addSubview:self.bannerContainer];
}
}
//Handle the in app purchases
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
//iAd
if(![[NSUserDefaults standardUserDefaults] objectForKey:kInAppPurchaseNoAds] && !self.bannerContainer){
self.bannerContainer.delegate = self;
}
if(self.bannerContainer.bannerLoaded){
[self showBanner];
}
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(checkIAdPurchase) name:IAPHelperProductPurchasedNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self hideBanner];
self.bannerContainer.delegate = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self name:IAPHelperProductPurchasedNotification object:nil];
}
#pragma mark Check iAd purchase
-(void)checkIAdPurchase
{
if([[NSUserDefaults standardUserDefaults] objectForKey:kInAppPurchaseNoAds] && self.bannerContainer){
[self hideBanner];
[self.bannerContainer removeFromSuperview];
self.bannerContainer.delegate = nil;
self.bannerContainer = nil;
}
}
#pragma mark IAd delegate
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
[self showBanner];
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
[self hideBanner];
}
#pragma mark Show and hide the banner
- (void)showBanner
{
if(!self.isBannerVisible){
[self.view layoutIfNeeded];
[UIView animateWithDuration:0.5
animations:^{
//Restore the constraint
self.mainViewBottomConstraint.constant = 50;
//Move the banner on
self.bannerContainer.frame = CGRectOffset(self.bannerContainer.frame, 0, -50);
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
self.isBannerVisible = YES;
}];
}
}
- (void)hideBanner
{
if(self.isBannerVisible){
[self.view layoutIfNeeded];
[UIView animateWithDuration:0.5
animations:^{
//Restore the constraint
self.mainViewBottomConstraint.constant = 0;
//Move the banner off
self.bannerContainer.frame = CGRectOffset(self.bannerContainer.frame, 0, self.bannerContainer.frame.size.height);
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
self.isBannerVisible = NO;
}];
}
}
Actually you are creating instance of iAd in AppDelegate and that's only one in the whole app.Now when you first time come to Vc then due to code in viewdidload it add to your Vc..
Now when you move to other view controller that viewcontroller also add iAd at viewdidload..You have only one object of iAd so it will add to at a time in One VC..So when you move to other Vc iAd will remove from that Vc and add to new VC..
Solution : You should call iAD subview code in ViewWillAppear...

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

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];

iAD View Freezes On Ad Close

Ok, so after testing on the simulators it turns out that this issue is only occurring on actual devices... How would I fix this?
I am making an iOS app with SpriteKit and am implementing iAD. Everything works more or less like I expect it too, except for one thing. When I tap on the ad, it brings me to a full screen ad, as expected, but when I close that ad the current view freezes, as in nothing happens visually. I know the app is still running because when I click the banner ad again and close out of it again the game returns to normal and the game had progressed while visually frozen. This is how the banner is initialized in my view controller class (the iAD delegate):
self.canDisplayBannerAds = YES;
CGRect bannerFrame = CGRectMake(0, 20, scene.size.width, 50);
banner = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];
banner.delegate = self;
banner.frame = bannerFrame;
[banner setAlpha:0];
[self.view addSubview:banner];
And these are the loading methods, also in the view controller class:
- (void) bannerViewDidLoadAd:(ADBannerView *) bannerM
{
NSLog(#"Ad Loaded");
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[bannerM setAlpha:1];
[UIView commitAnimations];
}
- (void) bannerView:(ADBannerView *)bannerM didFailToReceiveAdWithError:(NSError *)error
{
NSLog(#"Ad Failed");
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[bannerM setAlpha:0];
[UIView commitAnimations];
}
I don't really understand the problem or why it is happening... Any help would be appreciated!
Thanks,
StrongJoshua
EDIT Here are the two methods that are called when the banner ads open and close:
- (BOOL) bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
if(playing)
{
NSLog(#"Ad Pause");
SKView *v = (SKView *)self.view;
NSLog(#"2");
SKScene *s = (SKScene *)v.scene;
NSLog(#"3");
WBPlayScene *p = (WBPlayScene *) s;
NSLog(#"4");
[p.logic pause:YES];
NSLog(#"Done");
}
return YES;
}
- (void) bannerViewActionDidFinish:(ADBannerView *)banner
{
if(playing)
{
NSLog(#"Ad Unpause");
[((WBPlayScene *)((SKView *)self.view).scene).logic pause:NO];
}
}
FIXED
The reason for all those NSLogs is because the game crashes when I try to pause it. The game crashes after "2" is reached, so at SKScene *s = (SKScene *)v.scene;. It gives the error [UIView scene]: unrecognized selector sent to instance and I don't understand why...
Solution: To fix this side issue I changed self.view to self.originalContentView and it got the SKView instead of the ad banner's view.
Thanks very much for your help!
SOLVED: I had to remove the call to enable ad display self.canDisplayBannerAds because it interfered with my self-created banner.

changing iAd banners position

Im struggling to make an iAdBanner appear at the top of the screen instead of the bottom. In the Storyboard I've placed the AdBanner at the top but when the App loads it still appears on the bottom of the screen, however, when I click the Ad and then exit it, the iAdBanner moves to the top of the screen as intended. Any help with getting its default location to the top of the screen is appreciated.
Here is the code that I'm currently using to trigger the add.
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[banner setAlpha:1];
[UIView commitAnimations];
NSLog(#"Ad Displayed");
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[banner setAlpha:0];
[UIView commitAnimations];
NSLog(#"Ad Failed To Display");
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Configure the view.
SKView * skView = (SKView *)self.view;
skView.showsFPS = YES;
skView.showsNodeCount = YES;
// Create and configure the scene.
SKScene * scene = [AlienGravityGame sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
// Present the scene.
[skView presentScene:scene];
self.canDisplayBannerAds = YES;
}

Hide/Show iAds in Spritekit

I've been trying to figure out how to hide and show iAds in my Spritekit Scenes. Currently I have it setup like this:
ViewController.h
#import <UIKit/UIKit.h>
#import <SpriteKit/SpriteKit.h>
#import <iAd/iAD.h>
#interface ViewController : UIViewController <ADBannerViewDelegate> {
ADBannerView *adView;
}
-(void)showsBanner;
-(void)hidesBanner;
#end
ViewController.m
#import "ViewController.h"
#import <UIKit/UIKit.h>
#import <iAd/iAD.h>
#import "MyScene.h"
#import <SpriteKit/SpriteKit.h>
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Configure the view.
SKView * skView = (SKView *)self.view;
skView.showsFPS = NO;
skView.showsNodeCount = NO;
// Create and configure the scene.
SKScene * scene = [MyScene sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
// Present the scene.
[skView presentScene:scene];
self.canDisplayBannerAds = YES;
adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
adView.frame = CGRectOffset(adView.frame, 0, 0.0f);
adView.delegate=self;
[self.view addSubview:adView];
self.bannerIsVisible=NO;
}
-(void)bannerViewDidLoadAd:(ADBannerView *)banner {
if (!self.bannerIsVisible) {
[UIView beginAnimations:#"animatedAdBannerOn" context:NULL];
banner.frame = CGRectOffset(banner.frame, 0.0, 0.0);
[UIView commitAnimations];
self.bannerIsVisible = YES;
}}
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
if (!self.bannerIsVisible) {
[UIView beginAnimations:#"animatedAdBannerOff" context:NULL];
banner.frame = CGRectOffset(banner.frame, 0.0, 0.0);
[adView setAlpha:0];
[UIView commitAnimations];
self.bannerIsVisible = NO;
}
}
-(void)hidesBanner {
NSLog(#"HIDING BANNER");
[adView setAlpha:0];
self.bannerIsVisible = NO;
}
-(void)showsBanner {
NSLog(#"SHOWING BANNER");
[adView setAlpha:1];
self.bannerIsVisible = YES;
}
etc...
#end
Then in my scene I grab my viewcontroller with a pointer:
ViewController *controller;
controller = [[ViewController alloc] init];
[controller hidesBanner];
My nslog runs in the console so I know it's going through. But the banner won't hide. Any thoughts? I'm pretty new with objective c so I have a feeling I'm just doing something dumb.
Like Huygamer said, you're creating a new instance of a view controller so when you call your method [controller hidesBanner]; you're referring to another object.
The best approach here is to use NSNotificationCenter: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/nsnotificationcenter_Class/Reference/Reference.html
And send a message to your viewcontroller whenever you want to hide or show your ad:
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
//Add view controller as observer
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(handleNotification:) name:#"hideAd" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(handleNotification:) name:#"showAd" object:nil];
// Configure the view.
SKView * skView = (SKView *)self.view;
skView.showsFPS = NO;
skView.showsNodeCount = NO;
// Create and configure the scene.
SKScene * scene = [MyScene sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
// Present the scene.
[skView presentScene:scene];
self.canDisplayBannerAds = YES;
adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
adView.frame = CGRectOffset(adView.frame, 0, 0.0f);
adView.delegate=self;
[self.view addSubview:adView];
self.bannerIsVisible=NO;
}
//Handle Notification
- (void)handleNotification:(NSNotification *)notification
{
if ([notification.name isEqualToString:#"hideAd"]) {
[self hidesBanner];
}else if ([notification.name isEqualToString:#"showAd"]) {
[self showBanner];
}
}
And in your scene:
[[NSNotificationCenter defaultCenter] postNotificationName:#"showAd" object:nil]; //Sends message to viewcontroller to show ad.
[[NSNotificationCenter defaultCenter] postNotificationName:#"hideAd" object:nil]; //Sends message to viewcontroller to hide ad.
Of course, there are 2 object and why you think it can do?
If you want to access the parent of skscene just do this
UIViewController *vc = self.view.window.rootViewController;
You can access the parent of this skscene and you can do hideBanner at the parent of this scene. Simple?
Here is what I did to make it work with SpriteKit Scenes (Xcode 6.1 and iOS 8.1 on iPhone 6):
Step 1- Add #import <"iAd/iAd.h"> in MyScene.h header file
Step 2- Make sure you declare your MyScene class to implement protocol in MyScene.h header file.
Step 3- Add the following code lines in your MyScene.m file inside -(Void)didMoveToView:(SKView *)view function.
ADBannerView* banner=[[ADBannerView alloc]initWithFrame:CGRectZero];
CGRect bannerFrame =CGRectMake(0, 667, self.view.frame.size.width, 0);
banner.frame=bannerFrame;
[self.view addSubview:banner];
banner.delegate=self;
Step 4- Implement the two methods of iAd
-(void)bannerViewDidLoadAd:(ADBannerView *)banner
{
CGRect bannerFrame =CGRectMake(0, 667-50, self.view.frame.size.width, 0);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
banner.frame=bannerFrame;
[UIView commitAnimations];
}
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
CGRect bannerFrame =CGRectMake(0, 667, self.view.frame.size.width, 0);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
banner.frame=bannerFrame;
[UIView commitAnimations];
}
The above code will move the Ad frame to scene when there is an Ad and will remove the frame if no Ad by animating the movement. Note that the last number in the frame rect is 0. It doesn't matter what you put there, the banner hight is fixed and doesn't change (50 pt).
Step 5- Respond to Ad actions by this code:
-(void)bannerViewActionDidFinish:(ADBannerView *)banner
{
[self startTimer];
}
-(BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
[gameTimer invalidate];
return YES;
}
This code will stop the game timer when a user clicks on the banner and resumes the game timer after the user returns back to the game. You can add your own code for saving and retrieving game data here.
Hope this helps.

Resources