0

我有一个使用应用内购买的 iOS 应用。

它使用消耗型产品,当用户购买时,允许他/她将音频上传到服务器。

一旦 InAppPurchase 成功,数据就会上传到服务器。

InAppPurchase成功但上传失败的情况如何处理?我们不应该让用户再次购买产品,所以我将上传是否成功存储在用户默认值中。

这是代码

SKProduct *productToBuy = //product to buy.
if ([[[NSUserDefaults standardUserDefaults] objectForKey:productToBuy.productIdentifier] boolValue]) //if the user has already bought the product and it failed to upload, then just try to upload without buying..
{
    NSLog(@"already purchased, uploading");
    [self uploadRecording];
}
else //if not, then make the user purchase and then upload
{
    [[CGInAppPurchaseManager sharedManager] buyProduct:productToBuy withCompletionBlock:^(SKPaymentTransaction *paymentTransaction, BOOL success) {
        if (success)
        {
            [self uploadRecording];
        }
        else
        {
            if (paymentTransaction.error.code != SKErrorPaymentCancelled)
            {
                NSLog(@"Cancelled transaction");
            }
        }
    }];
}

uploadRecording 代码如下所示

- (void)uploadRecording
{
    NSURL *urlOfAudio = //url of audio file

    [self.model uploadAdOrDedicationWithTitle:title audioData:[NSData dataWithContentsOfURL:urlOfAudio] withCompletionBlock:^(id obj, NSInteger errCode, NSString *errorDescription) {
    SKProduct *productToBuy = self.adParams[@"product"];
    if (obj)
    {
        //if its successfully uploaded set false that the user has already bought a product with this identifier
        [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:NO] forKey:productToBuy.productIdentifier];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    else
    {
        //when there is an error set true that the user has already bought a product with this identifier
        [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:YES] forKey:productToBuy.productIdentifier];
        [[NSUserDefaults standardUserDefaults] synchronize];                                                

        //show appropriate error message
        //"An error occured while uploading your broadcast. You will not be made to purchase the product again the next time you try to upload a recording for the same product."
    }
  }];
}

这个可以吗?或者有没有更好的方法来处理这个?

4

1 回答 1

0

理想情况下,您应该退款,但这是不可能的,因此您只能尝试最小化并以最佳方式解决问题。您的解决方案似乎是您至少可以做的补救措施。此外,如果有办法进行一些验证以检查上传是否可能在实际上传之前运行(使用可达性 API或更好地连接到您的服务器以进行快速可用性检查),那么这将最大限度地降低风险发生这种情况。

于 2013-06-17T11:26:05.870 回答