Search code examples
objective-cin-app-purchaseiad

How do I remove ads with In-App Purchase?


I am having trouble finding out why my iAd is not being removed after a purchase.

Here is what I am trying in the file that has the iAd:

#define kInAppPurchaseUpgradeProductId @"kInAppPurchaseUpgradeProductId"

NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
BOOL adsAreOff = [defaults boolForKey:kInAppPurchaseUpgradeProductId];

if (!adsAreOff)
{
    adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
    adView.frame = CGRectOffset(adView.frame, 0, -50);
    adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifierPortrait];
    adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
    CGRect adFrame = adView.frame;
    adFrame.origin.y = self.view.frame.size.height-adView.frame.size.height;
    adView.frame = adFrame;
    adView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:adView];
    adView.delegate=self;
    self.bannerIsVisible=NO;
}
else 
{
    [adView removeFromSuperview];
}

Here is what I am trying in my In-App Purchase code:

-(void)purchaseUpgrade
{
    SKPayment * payment = [SKPayment paymentWithProductIdentifier:kInAppPurchaseUpgradeProductId];
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    [[SKPaymentQueue defaultQueue] addPayment:payment];
}

-(IBAction)purchaseButtonTapped
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:adView forKey:kInAppPurchaseUpgradeProductId];

    // I tried this first, didn't work
    //[defaults setBool:TRUE forKey:kInAppPurchaseUpgradeProductId];

    [defaults synchronize];

    [self purchaseUpgrade];
}

Is this handled in the right place, or should it be in the paymentQueue method?


Solution

  • Make sure that you're synchronizing your NSUserDefaults when you set the key after the application is purchased:

    // After a purchase is made:
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:SOMETHING forKey:kInAppPurchaseUpgradeProductId]
    [defaults synchronize];