使用 restoreCompletedTransactions 在新设备上恢复应用内购买/在同一设备上删除应用

Restore In App Purchases on New Devices / Deleted Apps on the same device using restoreCompletedTransactions

我在 App Store 中有一个应用程序,我将 IAPs 用于解锁新版本中的功能。我已经为两种非消耗品实施了 IAP 机制,所有购买都运作良好。

当用户购买产品时,我会设置一个 NSUserDefault 标志,然后使用它来解锁应用中其他地方的功能。

这很适合我。

我的问题是关于恢复。

我看到很多关于此的问题,但我不太清楚解决方案是什么。

如果用户购买了IAP,它会存储NSUserDefault标志。但是,如果用户使用具有相同 Apple ID 的不同设备,或者删除并重新安装应用程序,则恢复购买按钮不会检测到之前保存的 NSUserDefault(可以理解)。

- (void)restoreThePurchase
{
    // Similar to the purchase
    // Ask the App Store to restore rather than create a payment. We call it via the SKPaymentQueue.
    // When we've sent that ot the App Store, the App Store will get back to the App Delegate and get back to the paymentQueue updated:Transactions to the restore.
    [[SKPaymentQueue defaultQueue]restoreCompletedTransactions];
}

我有三个类:

当按下恢复按钮时,我检查设备有哪个 IAP,然后调用适当的恢复方法。

- (IBAction)iapRestorePurchaseButton:(id)sender
{
    // When the restorePurchase button is tapped, we will run a few tests.

    if (([[NSUserDefaults standardUserDefaults] boolForKey:@"FullVersion100Entries"]) && (![[NSUserDefaults standardUserDefaults] boolForKey:@"FullVersionUnlimitedEntries"]))

    {
        [self.e100EntriesPurchase restoreThePurchase];
        NSLog(@"100 yes but unlmited no restore!!!!!!!!!!!!!!!!!!!!!");
    }
    else if ((![[NSUserDefaults standardUserDefaults] boolForKey:@"FullVersion100Entries"]) && ([[NSUserDefaults standardUserDefaults] boolForKey:@"FullVersionUnlimitedEntries"]))
    {
        [self.eUnlimitedEntriesPurchase restoreThePurchase];


    }
    else
    {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Restore Unsuccessful" message:@"Your Apple ID doesn't have an active purchase for upgrading to Pro. " delegate:nil cancelButtonTitle:@"Thanks" otherButtonTitles:nil, nil];
        [alertView show];
    }

}

所以当我删除并重新安装应用程序时,我面临 UIAlertView 因为 NSUserDefaults 不会被设置为任何东西。

我不确定具体该怎么做,但我怀疑当我进行购买时,我需要做的不仅仅是 NSUserDefault 并且需要保存某种收据?

关于这个问题的任何指导都会很有帮助。

当用户点击 "Restore purchase" 按钮时,您应该这样做:

[[SKPaymentQueue defaultQueue]restoreCompletedTransactions];

新交易将被推送到您的付款队列,状态为 "restored",应用应将它们视为任何新购买的交易。