1

我已经通过应用内购买成功地从应用中删除了广告。

问题是,如果我关闭应用程序并重新打开。广告又开始了。

我有两个主要场景。GameOverScene 和 GameScene。应用内购买发生在 GameOverScene 中。

GameOverScene.m:

- (void)OnRemoveADS {
    [self showPurchaseAlert: IAP_Q_RemoveADS  :0];

    g_bRemoveADS = [[NSUserDefaults standardUserDefaults] boolForKey: @"REMOVEADS"];

    // For HZInterstitialAd, HZVideoAd, and HZIncentivizedAd, just check the BOOL to see if an ad should be shown
    if (!g_bRemoveADS) {
        [HZInterstitialAd show];

        [self removeBannerAds];
        [self disableAds];
        NSLog(@"Disable ads is called");
    }
}

- (void)removeBannerAds {
    HZBannerAdOptions *options = [[HZBannerAdOptions alloc] init];

    [HZBannerAd placeBannerInView:self.view
                         position:HZBannerPositionBottom
                          options:options
                          success:^(HZBannerAd *banner) {
                              if (g_bRemoveADS) { // case (2)
                                  // Just discard the banner
                                  [banner setHidden: YES];
                                  [banner removeFromSuperview];
                                  banner = nil;

                                  //_currentBannerAd = banner;

                                  NSLog(@"Banner ad removed!GameOverScene");
                              } else {
                                  // Keep a reference to the current banner ad, so we can remove it from screen later if we want to disable ads.
                                  _currentBannerAd = banner;
                              }
                              NSLog(@"Ad Shown! GameOverScene");
                          } 
                          failure:^(NSError *error) {
                              NSLog(@"Error = %@",error);
                          }];
}

- (void)disableAds {
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"REMOVEADS"];
    [_currentBannerAd removeFromSuperview]; // case (3)
}

游戏场景.m:

-(id) init {
    if (self = [super init]) {
        if (!g_bRemoveADS) {
            g_bRemoveADS=FALSE;
            [[NSUserDefaults standardUserDefaults] setBool:g_bRemoveADS forKey:@"REMOVEADS"];
            [[NSUserDefaults standardUserDefaults] synchronize];
        } else {
            g_bRemoveADS=TRUE;
            [[NSUserDefaults standardUserDefaults] setBool:g_bRemoveADS forKey:@"REMOVEADS"];
            [[NSUserDefaults standardUserDefaults] synchronize];
        }
    }
}

我试图解决它的方法是使用 AppDelegate.m 中 GameOverScene.m 中的相同代码,这样当应用程序启动时它将删除广告。

AppDelegate.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    g_bRemoveADS = [[NSUserDefaults standardUserDefaults] boolForKey: @"REMOVEADS"];

    if (!g_bRemoveADS) {

        [HZInterstitialAd show];

        [self disableAds];
        NSLog(@"Disable ads is called");
    }
}
4

2 回答 2

1

在我看来,你有一个否定。

if (!g_bRemoveADS) {应该if (g_bRemoveADS) {在 GameOverScene.m 中替换为。

if (g_bRemoveADS) {
    [HZInterstitialAd show];

    [self removeBannerAds];
    [self disableAds];
    NSLog(@"Disable ads is called");
}

g_bRemoveADS评估到TRUE何时设置了相应的用户默认值。设置后,您调用 removeBannerAds 东西等,这似乎是停用操作。

于 2015-10-06T13:05:41.237 回答
0

在更改 disableAds 方法中的值后,您必须同步您的 NSUserDefaults:

[[NSUserDefaults standardUserDefaults]synchronize];
于 2015-09-27T16:05:55.853 回答