ios - 在应用内购买后重新加载 SKScene 或 View 以删除 iAd

标签 ios objective-c xcode sprite-kit iad

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

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

In App Purchase 是在一个名为 PurchasedViewController 的单独类中进行的,我以模态方式呈现该类。

目前,我正在尝试在购买完成后向 Root View Controller 发送通知。

这是我的代码:

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

我不会在这里放置太多的应用程序内购买代码,因为我知道它可以工作,而且我已经成功地删除了 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 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 View ,将其设置为隐藏,并将其从其父 View 中删除(它从未添加到其中)。 iAd View 的实际“实时”实例不受此影响。

您需要有一个对现有 iAd View 的引用 (ivar):

@implementation ViewController
{
    ADBannerView* _adView;
}

在创建广告横幅 View 时分配引用:

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

然后使用引用删除广告横幅 View :

[_adView removeFromSuperview];
_adView = nil;

关于ios - 在应用内购买后重新加载 SKScene 或 View 以删除 iAd,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25862910/

相关文章:

iphone - 使用 Wsdl2Objc 的 iPhone 上的 Web 服务?

iphone - Xcode - 获取所有方法头的列表

XCode 7. iOS 模拟器丢失且无法安装

iphone - 根据设备类型选择不同的 Storyboard

ios - 检查发生了哪种 NSURLSessionTask

ios - PhoneGap 相机 API 不适用于 iOS

ios - Tinder 在 Objective-c 中加载照片的算法。用 Parse.com 复制它

ios - 范围超出范围大小 4 objective-c

ios - UITableViewCell 不使用不推荐使用的方法 initWithFrame :reuseIdentifier

ios - 如何从 View Controller 移动到 skscene