如何通过应用内购买删除广告?

时间:2012-06-24 02:29:42

标签: objective-c in-app-purchase iad

我无法找到购买后我的iAd未被移除的原因。

以下是我在具有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];
}

以下是我在应用内购买代码中尝试的内容:

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

这是在正确的地方处理,还是应该在paymentQueue方法中处理?

1 个答案:

答案 0 :(得分:1)

确保在购买应用程序后设置密钥时同步NSUserDefaults

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