ios - 在App Purchase中可以正常使用,但是付款成功后我无法更改UIButton图像

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

} 

在我重新启动应用程序后,该方法就可以工作,但是我希望在应用程序内购买完成后立即更改按钮图像。

我如何做到这一点?

谢谢!

最佳答案

当该值更改时,您需要向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];

    }
}

有呀!

关于ios - 在App Purchase中可以正常使用,但是付款成功后我无法更改UIButton图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8109984/

相关文章:

ios - 加载非 http ://url scheme 后的操作

ios - 无法上传 iApp 托管内容

ios - 应用程序在尝试恢复产品后每次启动时都要求登录

testing - Xcode 12 iOS 14 .storekit 恢复购买测试

ios 应用内购买 : When and why does SKPaymentTransactionStateRestored get sent back as status of a transaction?

ios - 按子实体属性过滤的核心数据谓词

ios - 删除部分图像 iOS

ios - 展开可选值 uitextfield 时意外发现 nil

ios - 进行inApp购买的用户的UDID

iphone - 通过iOS免费进行非消耗性应用内购买