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

时间:2015-03-30 12:47:05

标签: ios objective-c iphone in-app-purchase restore

我在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];
}

我有三个班级:

  • IAPViewController包含购买和恢复UIButton
  • e100EntriesPurchase包含IAP方法,例如进行购买和恢复。
  • eUnlimitedEntriesPurchase,其中包含无限购买的IAP方法以及make购买和恢复。

按下恢复按钮时,我会检查设备有哪个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并需要保存某种收据?

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

1 个答案:

答案 0 :(得分:0)

当用户点击“恢复购买”按钮时,您应该执行以下操作:

[[SKPaymentQueue defaultQueue]restoreCompletedTransactions];

新的交易将被推送到您的支付队列,状态为“已恢复”,应用应将其视为任何新购买的交易。