在应用内购买后重新加载SKScene或View以删除iAd [英] Reloading an SKScene or View to remove iAd after In App Purchase

查看:92
本文介绍了在应用内购买后重新加载SKScene或View以删除iAd的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在应用内购买之后,我已经成功从Sprite Kit游戏中删除了iAd,但是问题是该应用需要重新启动才能停止展示.

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.

这使我相信,需要在应用内购买完成后以某种方式刷新或重新加载视图或场景(我已经尝试了很多方法),以使iAd消失.

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.

应用内购买是在一个单独的类PurchadViewController中进行的,该类我以模态形式呈现.

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

目前,我正尝试在购买回购给根View Controller之后发送通知.

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

这是我的代码:

ViewController.m

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

PurchasedViewController.m

我不会在此处放置很多应用内购买代码,因为我知道它可以工作,并且我也已经成功删除了Chartboost的广告.

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

推荐答案

这不会删除iAd视图:

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

它的作用:创建一个新的iAd视图,将其设置为隐藏状态,并将其从其超级视图中删除(从未添加到该视图). iAd视图的实际活动"实例不受此影响.

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.

您需要引用一个现有的iAd视图(ivar):

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;

这篇关于在应用内购买后重新加载SKScene或View以删除iAd的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆