我有一个使用In App Purchase的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."
}
}];
}
这可以吗?或者有更好的方法来解决这个问题吗?
答案 0 :(得分:0)
理想情况下,您应该退款,但这是不可能的,因此您不得不尝试最小化并以最佳方式解决问题。您的解决方案是一种似乎至少可以做到的补救措施。此外,如果有办法进行一些验证,以检查上传是否可能在实际上传之前运行(使用the Reachability API或更好仍然连接到您的服务器以进行快速可用性检查),那么这将最小化发生这种情况的风险。