How do I remove ads with In-App Purchase?

[亡魂溺海] 提交于 2019-12-12 09:27:04

问题


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?


回答1:


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];


来源:https://stackoverflow.com/questions/11174552/how-do-i-remove-ads-with-in-app-purchase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!