IAP恢复按钮

时间:2014-08-16 18:52:13

标签: ios in-app-purchase

我正在使用非耗材IAP并尝试让“恢复”按钮正常工作。

我注意到,当用户尝试购买他们已经购买的商品时,会出现Apple警告说“你已经购买了这件商品”。你想免费获得它吗?"

所以我想知道如何为恢复按钮使用相同的确认收据验证方法/代码?

我似乎无法找到它的代码。我看过paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions和其他地方了吗?

你能帮帮忙吗?谢谢!

更新

- (IBAction)purchaseRestore:(id)sender {
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    [[SKPaymentQueue defaultQueue]restoreCompletedTransactions];
    // Stack
    [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
    NSLog(@"4 IBAction Purchase Restore Method");
}

更新2 我无法判断我是否正确执行此操作,将某些代码加倍,或添加不需要的代码。

-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
    for (SKPaymentTransaction *transaction in transactions) {
        switch (transaction.transactionState) {
            case SKPaymentTransactionStatePurchasing:
                NSLog(@"2 Payment Queue updatedTransactions - needs to verify logged in user: Purchasing Product From Store!");
                break;
            case SKPaymentTransactionStatePurchased:
                if ([transaction.payment.productIdentifier
                     isEqualToString:INAPP_PRODUCT_ID_3]) {
                    NSLog(@"3 Payment Queue updatedTransactions - Product Purchased From Store!");
                    //Not sure if I need this
                    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:
                                              @"3 Payment Queue updatedTransactions - Seems to to this twice: Purchase is completed succesfully" message:nil delegate:
                                              self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
                    [alertView show];

                    [self saveTransactionReceipt:transaction];
                    NSLog(@"3 Payment Queue updatedTransactions - Save Transaction Receipt: Called after product purchased");
                    [self saveValue:transaction];
                    NSLog(@"3 Payment Queue updatedTransactions - Save Value: Called after product purchased");
                    [self showButtonThree];
                    NSLog(@"3 Payment Queue updatedTransactions: will Show button Three now");

                    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
                    [defaults setBool:YES forKey:@"purchasedThree"];
                    NSLog(@"updatedTransactions Yes for purchasedTeamThree: %hhd", [defaults boolForKey:@"purchasedThree"]);
                    [defaults synchronize];

                    [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
                    NSLog(@"3 Payment Queue finishTransaction: Run");
                }
                break;
            case SKPaymentTransactionStateRestored:
                    NSLog(@"Restored");
                    [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
                break;
            case SKPaymentTransactionStateFailed:
                purchaseButton.enabled = YES;
                NSLog(@"2 Payment Queue updatedTransactions: Purchase failed ");
                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
                break;
            default:
                break;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

你的问题对我来说实际上也非常有用,因为你让我意识到我在AppStore的批准过程中暂时待定的app可能会被拒绝,因为我没有实现一个Restore按钮(我刚添加了一个标签说再次按下购买按钮将恢复之前的购买。)

执行还原过程的正确方法是实现一个调用此方法的按钮:

[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];

之后,当然,恢复应用功能仅限于付费用户。

希望这有帮助。