【问题标题】:Reloading an SKScene or View to remove iAd after In App Purchase在应用内购买后重新加载 SKScene 或视图以删除 iAd
【发布时间】:2014-09-16 07:25:39
【问题描述】:

在应用内购买后,我已成功从我的 Sprite Kit 游戏中删除 iAd,但问题是需要重新启动应用才能停止显示广告。

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

应用内购买是在一个名为 PurchasedViewController 的单独类中进行的,我以模态方式呈现。

目前,我正在尝试在购买完成后向根视图控制器发送通知。

这是我的代码:

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

我不会在这里放太多 In App Purchase 代码,因为我知道它可以工作,而且我已经成功删除了 Chartboost 的广告。

-(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 视图已被移除并解除分配。检查 Instruments,看看 iAd 视图是否仍然“活跃”。
  • 我在[adView removeFromSuperview]; 之后尝试了adView.delegate = nil;。如何在代码中取消分配 iAd?
  • 等等,我现在看到了...
  • 当我在购买后关闭PurchasedVC 模式并返回到 SKScene 我在控制台中收到NSLog(@"iAds have been removed");。另外,我尝试过 Instruments,但我真的不知道如何完全使用它。我正在使用 Allocations,但没有找到任何对 iAd 或 iAd 框架的引用。

标签: ios objective-c xcode sprite-kit iad


【解决方案1】:

这不会删除 iAd 视图:

 //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 视图的实际“实时”实例不受此影响。

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

@implementation ViewController
{
    ADBannerView* _adView;
}

在创建广告横幅视图时分配参考:

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

稍后使用引用删除广告横幅视图:

[_adView removeFromSuperview];
_adView = nil;

【讨论】:

  • 这是有道理的。我认为创建两次可能有问题。我试试看。
  • @LearnCocos2D,但是如何在购买后重新加载场景以便我们可以管理帧。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-05
  • 2012-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多