1

I have successfully removed ads from the app with an in app purchase.

The problem is that if I close the app and reopen. The ads start up again.

I have 2 main scenes. The GameOverScene and the GameScene. The In App Purchase happens in the 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)
}

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

The way I'm trying to solve it is by using the same code from the GameOverScene.m in the AppDelegate.m that way when the app starts up it will remove the ads.

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");
    }
}
Hong Duan
  • 4,234
  • 2
  • 27
  • 50
Paul
  • 1,179
  • 3
  • 14
  • 38
  • The if else in the init is not necessary. It looks like you have two different types of ad. Did you update NSUserDefault after iap completed? – user523234 Sep 27 '15 at 10:37
  • Yeah, you're right. I commented it out. I figure something needs to happen in the App Delegate when the app starts up again? – Paul Sep 27 '15 at 19:21
  • I did a `[[NSUserDefaults standardUserDefaults]synchronize];` like in the answer below, but then that didn't stop the ads from coming. Even after the In App Purchase. When I commented that line out and tested, the ads stopped. However, the issue remains that when I start the app again, it doesn't remember that the ads have been removed. – Paul Sep 27 '15 at 19:24
  • Theses two links might help: [How do you add an in-app purchase to an iOS application?](http://stackoverflow.com/questions/19556336/how-do-you-add-an-in-app-purchase-to-an-ios-application/19556337#19556337), [How do I remove ads with In-App Purchase?](http://stackoverflow.com/questions/11174552/how-do-i-remove-ads-with-in-app-purchase) – Caleb Kleveter Sep 29 '15 at 19:35
  • You need to check `if (!g_bRemoveADS)` wherever you're creating your ads and just **don't** create them. Where are you creating your ads? Where are your ads delegate methods? Are you recreating the ads in their delegate methods? – Daniel Storm Oct 06 '15 at 13:14

2 Answers2

1

Out of my perspective you have one negation to much.

if (!g_bRemoveADS) { should be replaced with if (g_bRemoveADS) { in GameOverScene.m.

if (g_bRemoveADS) {
    [HZInterstitialAd show];

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

g_bRemoveADS evaluates to TRUE when the respective user-default is set. When it is set, then you call the removeBannerAds stuff etc. which seems to be the deactivation action.

M156
  • 1,044
  • 1
  • 12
  • 29
  • Unfortunately, I tried that already. When I made the if statement positive, the ads wouldn't work at all. I see your reasoning in your answer though. Why it works with `if (!g_bRemoveADS)`? I don't know. – Paul Oct 07 '15 at 03:57
  • One thing that is bit confusing in your code, is that you disable ads BUT use the `[HZInterstitialAd show]` method - why is this? – M156 Oct 07 '15 at 09:28
  • Yes, I know it's confusing. I got the code snippet from a Heyzap engineer so that's why I'm using it. You're right that it doesn't make sense though. I should ask him about it. – Paul Oct 08 '15 at 18:07
  • The thing is if I remove that method, then the ads won't be stopped at all. – Paul Oct 08 '15 at 18:08
  • When you ask me, then your code is "cleaning up" your scene to show an interstitial ad. Meaning: It removes the banner ads, so they can't interfere with your interstitial ads. But I think this it totally besides your desired usecase. You want to disable ALL ads (interstitial + banner), when the respective flag in the user defaults is set, right? – M156 Oct 08 '15 at 18:24
  • Actually, the code currently only works to remove the interstitial ads. The banners remain. Also, when I restart the app the interstitials come back. Anyway, I gave up on this already as I just couldn't get it working with the Heyzap SDK – Paul Oct 08 '15 at 19:47
0

You have to synchronise your NSUserDefaults after changing changing a value in your disableAds method with:

[[NSUserDefaults standardUserDefaults]synchronize];
Jad Feitrouni
  • 637
  • 6
  • 12
  • I did this, but then the ads would not stop after that. When I removed `[[NSUserDefaults standardUserDefaults]synchronize];` then the ads would stop again. However, the original problem remains. When I start the app over, it doesn't remember to the In App Purchase. – Paul Sep 27 '15 at 19:14