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."
}
}];
}
これでいいですか?または、これを処理するためのより良い方法はありますか?