iOS in-app恢复购买的噩梦

时间:2015-03-15 18:17:56

标签: ios in-app-purchase restore storekit

我已经阅读了互联网上的所有线程等,当我尝试恢复购买的产品(沙箱环境)时,我仍然恢复零恢复交易。

我通过iTunes连接创建了3个非续订产品和2个沙盒测试程序。

我在设置实际购买产品的能力方面没有任何问题;一切都很好。

我创建了一个“恢复购买”按钮,并试图让它工作。我一直在测试以下内容:安装应用程序并进行购买。再次购买,它询问我是否要续订。删除应用并退出应用商店。重新安装应用程序并按恢复购买...零!!!

以下是我正在使用的一些代码:

-(void)viewDidLoad
{
    [super viewDidLoad];

    //in-app purchase setup
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
}
-(void)viewDidAppear:(BOOL)animated
{
    //make request for in-app products
    if([SKPaymentQueue canMakePayments])
    {
        SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObjects:@"product1_id",@"product2_id",@"product3_id",nil]];
        request.delegate = self;

        [request start];
    }
    else
    {
        //alert user
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"..." message:@"Please enable In App Purchase in settings" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }
}

-(IBAction)restoreTransactionsPressed
{
    _refreshRequest = [[SKReceiptRefreshRequest alloc] init];
    _refreshRequest.delegate = self;
    [_refreshRequest start];
}
-(void)requestDidFinish:(SKRequest *)request
{
    if([request isKindOfClass:[SKReceiptRefreshRequest class]])
    {
        NSLog(@"Got a new receipt... %@",request.description);
        [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
    }
}
-(void)request:(SKRequest *)request didFailWithError:(NSError *)error
{
    NSLog(@"%@",error.description);
}

-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
    NSLog(@"updatedTransactions");

    for(SKPaymentTransaction *transaction in transactions)
    {
        NSLog(@"updatedTransaction:%@ ... state:%@",transaction.originalTransaction.payment.productIdentifier,transaction.transactionState);

        switch(transaction.transactionState)
        {
            case SKPaymentTransactionStatePurchased:
                NSLog(@"SKPaymentTransactionStatePurchased");
                //unlock feature code
                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
                break;

            case SKPaymentTransactionStateFailed:
                NSLog(@"SKPaymentTransactionStateFailed");
                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
                break;

            case SKPaymentTransactionStateRestored:
                NSLog(@"SKPaymentTransactionStateRestored");
                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
                break;

            default:
                break;
        }
    }
}

-(void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue
{
    NSLog(@"paymentQueueRestoreCompletedTransactionsFinished");

    NSLog(@"received restored transactions: %i", (int)queue.transactions.count);

    for(SKPaymentTransaction *transaction in queue.transactions)
    {
        NSLog(@"transaction:%@",transaction);
    }
}
-(void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error
{
    NSLog(@"restoreCompletedTransactionsFailedWithError %@",error);
}

为什么我总是回归零恢复交易?

似乎“restoreCompletedTransactions”不会依次调用 - paymentQueue:updatedTransactions:如果有帮助的话。

我不确定SKReceiptRefreshRequest是否必要,但我已经尝试在恢复之前刷新收据,而不是在恢复之前刷新收据。

请不要发布链接到线程。我已经调查了几天了!有人声称Apple的Sandbox过去有问题......这可能是原因吗?先进的tyvm;所有的帮助表示赞赏!

1 个答案:

答案 0 :(得分:2)

根据Apple's documentation,您无法恢复非续订订阅。另外,请阅读here以获得更详细的解释。