问题
I am allowing user to purchase multiple consumables(of same type) at a time. I have implemented the following code:
- (void)purchaseMyProduct:(NSArray *) products {
if ([SKPaymentQueue canMakePayments]) {
for(SKProduct *product in products) {
SKPayment *payment = [SKPayment paymentWithProduct:product];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
}
else {
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:
@"Purchases are disabled in your device" message:nil delegate:
self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alertView show];
}
}
-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
UIAlertView *alertView ;
for (SKPaymentTransaction *transaction in transactions) {
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchasing:
NSLog(@"Purchasing");
break;
case SKPaymentTransactionStatePurchased:
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
[self
break;
default:
break;
}
}
}
But the problem is that for each single consumable entry a separate prompt is displayed to ask user to confirm the purchase.
Is it possible in IAP to purchase multiple consumables of same type at a time with one prompt for user?
One logic which I have thought is to create separate products in store for multiple consumables e.g. one product for two and another for three consumables.
Kindly help.
Thanks,
回答1:
If they are of the same type, you should use the quantity field. Check out the SKPayment class reference. The maximum value for quantity is 10.
To create a SKPayment object with a quantity greater than 1, create a SKMutablePayment object, adjust its quantity property and then add it to the payment queue.
Example from the docs:
SKMutablePayment *myPayment = [SKMutablePayment paymentWithProduct: myProduct];
myPayment.quantity = 2;
[[SKPaymentQueue defaultQueue] addPayment:myPayment];
来源:https://stackoverflow.com/questions/38817449/purchasing-multiple-consumables-at-a-time