Reloading an SKScene or View to remove iAd after I

2019-08-08 09:57发布

问题:

I've successfully removed iAds from my Sprite Kit game after an In App Purchase, but the problem is that the app needs to be restarted for the ads to stop showing.

This leads me to believe that the view or scene needs to be refreshed/reloaded somehow (I've tried so many ways) for the iAds to disappear after the In App Purchase is made.

The In App Purchase is made in a separate class called PurchasedViewController which I present modally.

Currently, I'm trying to send a notification after the purchase has been made back to the root View Controller.

Here's my code:

ViewController.m

#import "ViewController.h"
#import "Intro.h"
#import "PurchasedViewController.h"
#import <iAd/iAd.h>
#import "InAppManager.h"

@implementation ViewController

- (void)viewDidLoad {

  [super viewDidLoad];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(reload)
                                             name:@"reloadIntro"
                                           object:nil];

// Configure the view.
SKView * skView = (SKView *)self.view;


// Create and configure the scene.
SKScene *scene = [Intro sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;


 // Present the scene
 [skView presentScene:scene];
}

-(void)reload {
// Reload the View/Scene to remove iAds
 .....
}

- (void)viewDidAppear:(BOOL)animated {

   [super viewDidAppear:animated];

  //Display iAds
  if ( [[InAppManager sharedManager] isFeature1PurchasedAlready] == FALSE) {
    NSLog(@"iAds are showing");
    ADBannerView *adView = [[ADBannerView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 480, 320, 50)];
    [self.view addSubview:adView];
  }

 //Remove iAds
 else if ( [[InAppManager sharedManager] isFeature1PurchasedAlready] == TRUE) {

    NSLog(@"iAds have been removed");
    ADBannerView *adView = [[ADBannerView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 480, 320, 50)];
    adView.hidden = YES;
    [adView removeFromSuperview];
  }
}

PurchasedViewController.m

I won't put much of the In App Purchase code here, because I know it works and I've already successfully removed the ads for Chartboost as well.

-(void) unlockProduct1 {

  if ( [[InAppManager sharedManager] isFeature1PurchasedAlready] == NO) {
     NSLog(@"Product 1 was not bought yet");
    [buyProduct1Button setBackgroundImage:[UIImage imageNamed:@"Remove_Ads"] forState:UIControlStateNormal];
 }

  else {
    NSLog(@"Product 1 WAS bought");
    [buyProduct1Button setBackgroundImage:[UIImage imageNamed:@"Purchased"] forState:UIControlStateNormal];

    // Sending Notification to ViewController.m
    NSLog(@"Did Send notification reloadIntro");
    [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadIntro" object:nil];
  }
}

- (IBAction)dismissPurchasedVC:(UIButton *)sender {
    [self dismissModalViewControllerAnimated:YES];
 }

回答1:

This does not remove the iAd view:

 //Remove iAds
 else if ( [[InAppManager sharedManager] isFeature1PurchasedAlready] == TRUE) {

    NSLog(@"iAds have been removed");
    ADBannerView *adView = [[ADBannerView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 480, 320, 50)];
    adView.hidden = YES;
    [adView removeFromSuperview];
  }

What it does: it creates a new iAd view, sets it to hidden, and removes it from its superview (which it was never added to). The actual "live" instance of iAd view isn't affected by this.

You need to have a reference (ivar) to the one existing iAd view:

@implementation ViewController
{
    ADBannerView* _adView;
}

Assign the reference when you create the ad banner view:

_adView = [[ADBannerView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 480, 320, 50)];
[self.view addSubview:_adView];

And later use the reference to remove the ad banner view:

[_adView removeFromSuperview];
_adView = nil;