在App Purchase中工作正常,但是在付款成功后我无法更改UIButton图像

时间:2011-11-13 06:12:13

标签: ios in-app-purchase storekit

我的应用内购买和运行正常。这就是我在InAppPurchaseObserver.m中所拥有的:

case SKPaymentTransactionStatePurchased:
            // Item was successfully purchased!

            [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isProUpgradePurchased" ];
            [[NSUserDefaults standardUserDefaults] synchronize];

            [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
            break;

case SKPaymentTransactionStateRestored:
            // Verified that user has already paid for this item.
            // Ideal for restoring item across all devices of this customer.

            [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isProUpgradePurchased" ];
            [[NSUserDefaults standardUserDefaults] synchronize];

            [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
            break;

在我的ViewController中,我这样做:

if([[NSUserDefaults standardUserDefaults] boolForKey:@"isProUpgradePurchased"]) {

  [energeeButton setImage:[UIImage imageNamed:@"thumb_unlocked_off.png"] forState:UIControlStateNormal];

} 

一旦我重新启动应用程序就行了,但我希望在应用内购买完成后立即更改按钮图像。

我该如何实现?

谢谢!

1 个答案:

答案 0 :(得分:3)

您需要在更改该值时向viewcontroller发送排序通知,以便重新加载该特定图像

[[NSNotificationCenter defaultCenter] postNotificationName:@"DZCustomNotification" object:nil];
// put this right after you change the user defaults




// put this in the initializer of your viewcontroller
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(purchasedChanged:) name:@"DZCustomNotification" object:nil];

// create this instance method in your viewcontroller class

- (void)purchasedChanged:(NSNotification *)notif {
    if([[NSUserDefaults standardUserDefaults] boolForKey:@"isProUpgradePurchased"]) {

        [energeeButton setImage:[UIImage imageNamed:@"thumb_unlocked_off.png"] forState:UIControlStateNormal];

    }
}

你去!