アプリにアプリ内購入を実装しましたが、それについて 2 つの質問があります。これは私が使用するコードです:
- (IBAction)buyProduct1:(id)sender {
SKPayment *payment = [SKPayment paymentWithProduct:product1];
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
- (IBAction)restorePurchases:(id)sender {
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
for (SKPaymentTransaction *transaction in transactions) {
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchasing:
break;
case SKPaymentTransactionStatePurchased:
//Can I be 100% sure that this method is only called when the product is bought?
[self product1Bought:transaction];
break;
case SKPaymentTransactionStateRestored:
//What code should I have here to safely restore product1 if the product is bought?
break;
case SKPaymentTransactionStateFailed:
break;
default:
break;
}
}
}
- (void)product1Bought:(SKPaymentTransaction *)transaction {
NSString *string = @"Product1";
[[NSUserDefaults standardUserDefaults] setObject:string forKey:@"Product1"];
NSLog(@"Product1 is bought");
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
1. productBought メソッドが正常に購入された後にのみ呼び出され、奇妙なことが起こって製品が実際に購入されなかった場合にのみ呼び出されることを 100% 確信できるかどうか疑問に思っていました。
2.product1Bought メソッドを呼び出せるように、product1 が以前に購入されたかどうかを確認するにはどうすればよいですか?