Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
382 views
in Technique[技术] by (71.8m points)

ios - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...