我有一个使用应用内购买的 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."
}
}];
}
这个可以吗?或者有没有更好的方法来处理这个?