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,
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];
Related
I've read ALL the threads etc. on the internet and still am getting back zero restored transactions when I try to restore purchased products (sandbox environment).
I have created 3 Non-renewing products and 2 sandbox testers through iTunes connect.
I have had zero issues setting up the ability to actually purchase products; all is well.
I have created a "restore purchases" button and am trying to get it to work. I've been testing the following: install app and make purchase. Purchase again and it asks if I want to renew. Delete app and log out of app store. Reinstall app and press restore purchases ... zero!!!
Here's some code I'm using:
-(void)viewDidLoad
{
[super viewDidLoad];
//in-app purchase setup
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
}
-(void)viewDidAppear:(BOOL)animated
{
//make request for in-app products
if([SKPaymentQueue canMakePayments])
{
SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObjects:#"product1_id",#"product2_id",#"product3_id",nil]];
request.delegate = self;
[request start];
}
else
{
//alert user
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"..." message:#"Please enable In App Purchase in settings" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
}
-(IBAction)restoreTransactionsPressed
{
_refreshRequest = [[SKReceiptRefreshRequest alloc] init];
_refreshRequest.delegate = self;
[_refreshRequest start];
}
-(void)requestDidFinish:(SKRequest *)request
{
if([request isKindOfClass:[SKReceiptRefreshRequest class]])
{
NSLog(#"Got a new receipt... %#",request.description);
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}
}
-(void)request:(SKRequest *)request didFailWithError:(NSError *)error
{
NSLog(#"%#",error.description);
}
-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
NSLog(#"updatedTransactions");
for(SKPaymentTransaction *transaction in transactions)
{
NSLog(#"updatedTransaction:%# ... state:%#",transaction.originalTransaction.payment.productIdentifier,transaction.transactionState);
switch(transaction.transactionState)
{
case SKPaymentTransactionStatePurchased:
NSLog(#"SKPaymentTransactionStatePurchased");
//unlock feature code
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
case SKPaymentTransactionStateFailed:
NSLog(#"SKPaymentTransactionStateFailed");
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
case SKPaymentTransactionStateRestored:
NSLog(#"SKPaymentTransactionStateRestored");
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
default:
break;
}
}
}
-(void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue
{
NSLog(#"paymentQueueRestoreCompletedTransactionsFinished");
NSLog(#"received restored transactions: %i", (int)queue.transactions.count);
for(SKPaymentTransaction *transaction in queue.transactions)
{
NSLog(#"transaction:%#",transaction);
}
}
-(void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error
{
NSLog(#"restoreCompletedTransactionsFailedWithError %#",error);
}
Why do I always get back zero restored transactions?
it seems that "restoreCompletedTransactions" isn't in turn calling - paymentQueue:updatedTransactions: if that helps.
I'm unsure that SKReceiptRefreshRequest is necessary, but I've tried both refreshing the receipt before restoring and not refreshing the receipt before restoring.
Please don't post links to thread. I've investigated this for a few Days now! Some people claim that Apple's Sandbox had issues in the past ... could this be the reason? tyvm in advanced; all help is appreciated!
According to Apple's documentation, you can't restore non-renewing subscriptions. Also, have a read here for a more detailed explanation.
I am working on IAP first time. I have created test user and working good in sandbox env. I can see my Purchase and buy it as well(And of course i added StoreKit framework.), but problem is that: when i upload update with IAP to AppStore - moderator has reject my app, here is a reason:
We found that while you have submitted In App Purchase products for your app, the In App Purchase functionality is not present in your binary. Please see the attached screenshot/s for more information.(there is no some Purchase on the screen, empty space instead)
If you would like to utilize In App Purchase in your app, you will need to upload a new binary that incorporates the In App Purchase API to enable users to make a purchase
I just don't understand how it's possible? Why when i testing IAP - it's works fine, but it doesn't work when moderator doing it? All IAP have "Cleared for Sale" and "Waiting for review" status
Now if i upload app to the store will IAP works? Or maybe i need to do some additional action before?
Here is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
_iapArray = [[NSMutableArray alloc] init];
iap1 = #"com.mypurchase.addcoins1";
[self paymentCheck];
}
- (void)viewDidDisappear:(BOOL)animated
{
[[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
}
- (void)paymentCheck{
if([SKPaymentQueue canMakePayments]){
NSLog(#"User can make payments");
SKProductsRequest *productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObjects:iap1, nil]];
productsRequest.delegate = self;
[productsRequest start];
}
else{
NSLog(#"User cannot make payments due to parental controls");
//this is called the user cannot make payments, most likely due to parental controls
}
}
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
SKProduct *validProduct = nil;
int count = [response.products count];
if(count > 0){
_iapArray = response.products;
indicator.hidden = YES;
[self.tableView reloadData];
}
else if(!validProduct){
NSLog(#"No products available");
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Error"
delegate:nil cancelButtonTitle:#"OK" otherButtonTitles: nil];
[myAlertView show];
//this is called if your product id is not valid, this shouldn't be called unless that happens.
}
}
- (IBAction)purchase:(SKProduct *)product{
SKPayment *payment = [SKPayment paymentWithProduct:product];
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions{
for(SKPaymentTransaction *transaction in transactions){
switch (transaction.transactionState){
case SKPaymentTransactionStatePurchasing: NSLog(#"Transaction state -> Purchasing");
//called when the user is in the process of purchasing, do not add any of your own code here.
break;
case SKPaymentTransactionStatePurchased:
//this is called when the user has successfully purchased the package (Cha-Ching!)
// [self doRemoveAds]; //you can add your code for what you want to happen when the user buys the purchase here, for this tutorial we use removing ads
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
NSLog(#"Transaction state -> Purchased - %#", transaction.payment.productIdentifier);
if ([transaction.payment.productIdentifier isEqualToString:iap1]) {
[self addCoins:5];
}
break;
case SKPaymentTransactionStateRestored:
NSLog(#"Transaction state -> Restored");
//add the same code as you did from SKPaymentTransactionStatePurchased here
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
case SKPaymentTransactionStateFailed:
//called when the transaction does not finnish
if(transaction.error.code != SKErrorPaymentCancelled){
NSLog(#"Transaction state -> Cancelled");
//the user cancelled the payment ;(
}
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
}
}
}
As per their reply, you have integrated IAP in application but you have not used it. So, can you please make sure you have properly integrated IAP in application ? Please test in same Device and iOS if they mentioned.
I followed this tutorial to help me add in-app purchases. It works well, except now I am trying to add another non-consumable IAP. I defined my second product ID and created a method for the purchase button, but I'm not sure if I have to create another SKProductsRequest for this extra item, or if I use the same one, etc...
Here is my full code.
#define kRemoveAdsProductIdentifier #"099"
#define kDoubleEarningRateProductIdentifer #"199"
- (void)removeAds{
NSLog(#"User requests to remove ads");
if([SKPaymentQueue canMakePayments]){
NSLog(#"User can make payments");
SKProductsRequest *productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:kRemoveAdsProductIdentifier]];
productsRequest.delegate = self;
[productsRequest start];
}
else{
NSLog(#"User cannot make payments due to parental controls");
//this is called the user cannot make payments, most likely due to parental controls
}
}
- (void)doubleEarningRate{
NSLog(#"User requests 2x earning rate");
if([SKPaymentQueue canMakePayments]){
NSLog(#"User can make payments");
SKProductsRequest *productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:kDoubleEarningRateProductIdentifer]];
productsRequest.delegate = self;
[productsRequest start];
}
else{
NSLog(#"User cannot make payments due to parental controls");
//this is called the user cannot make payments, most likely due to parental controls
}
}
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
SKProduct *validProduct = nil;
int count = [response.products count];
if(count > 0){
validProduct = [response.products objectAtIndex:0];
NSLog(#"Products Available!");
[self purchase:validProduct];
}
else if(!validProduct){
NSLog(#"No products available");
//this is called if your product id is not valid, this shouldn't be called unless that happens.
}
}
- (void)purchase:(SKProduct *)product{
SKPayment *payment = [SKPayment paymentWithProduct:product];
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
- (void) restore{
//this is called when the user restores purchases, you should hook this up to a button
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}
- (void) paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue
{
NSLog(#"received restored transactions: %i", queue.transactions.count);
for (SKPaymentTransaction *transaction in queue.transactions)
{
if(SKPaymentTransactionStateRestored){
NSLog(#"Transaction state -> Restored");
//called when the user successfully restores a purchase
[self doRemoveAds];
[self doubleEarningRate];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
}
}
}
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions{
for(SKPaymentTransaction *transaction in transactions){
switch (transaction.transactionState){
case SKPaymentTransactionStatePurchasing: NSLog(#"Transaction state -> Purchasing");
//called when the user is in the process of purchasing, do not add any of your own code here.
break;
case SKPaymentTransactionStatePurchased:
//this is called when the user has successfully purchased the package (Cha-Ching!)
[self doRemoveAds]; //you can add your code for what you want to happen when the user buys the purchase here, for this tutorial we use removing ads
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
NSLog(#"Transaction state -> Purchased");
break;
case SKPaymentTransactionStateRestored:
NSLog(#"Transaction state -> Restored");
//add the same code as you did from SKPaymentTransactionStatePurchased here
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
case SKPaymentTransactionStateFailed:
//called when the transaction does not finnish
if(transaction.error.code != SKErrorPaymentCancelled){
NSLog(#"Transaction state -> Cancelled");
//the user cancelled the payment ;(
}
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
}
}
}
-(void)doRemoveAds
{
areAdsRemoved = true;
NSLog(#"Ads Removed");
[topBanner removeFromSuperview];
}
-(void)doDoubleEarningRate
{
}
I also read through apple documentation which explained to me what each part does, but I am still clueless on how I can add another purchase, and most other tutorials are done differently or outdated. Also all the variables and indirection is a bit intimidating to me. So I am hoping that someone can give me a quick step by step guide for adding another purchase.
To make it clearer, this approach works perfectly for just one in app purchase. However I don't know how to add more, as in I don't know how to make the program recognize which in-app purchase is being selected.
Thanks.
I fixed this by creating a method called provideContent:(NSString *)productId. Then you can either use a switch statement or if statement to differentiate between product Id's. No need to create multiple types of productRequest methods, etc...
I used info from this brilliant page to add in-app purchases.
However I get the NSLog No Product Available. I have checked to make sure I have added the correct Product ID. I am at a loss to what to do and have been at this for several hours.
This is for a game built on Cocos2d and I can't figure out what is wrong. Sorry for the abundance of code.
I have added <SKProductsRequestDelegate, SKPaymentTransactionObserver> and <StoreKit/StoreKit.h>
.m
#define kRemoveAdsProductIdentifier #"COINS1000"
#implementation shopCoins
- (void)buy500Coins{
NSLog(#"User requests to remove ads");
if([SKPaymentQueue canMakePayments]){
NSLog(#"User can make payments");
SKProductsRequest *productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:kRemoveAdsProductIdentifier]];
productsRequest.delegate = self;
[productsRequest start];
}
else{
NSLog(#"User cannot make payments due to parental controls");
//this is called the user cannot make payments, most likely due to parental controls
}
}
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
SKProduct *validProduct = nil;
int count = [response.products count];
if(count > 0){
validProduct = [response.products objectAtIndex:0];
NSLog(#"Products Available!");
[self purchase:validProduct];
}
else if(!validProduct){
NSLog(#"No products available");
//this is called if your product id is not valid, this shouldn't be called unless that happens.
}
}
- (IBAction)purchase:(SKProduct *)product{
SKPayment *payment = [SKPayment paymentWithProduct:product];
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
- (IBAction) restore{
//this is called when the user restores purchases, you should hook this up to a button
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}
- (void) paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue
{
NSLog(#"received restored transactions: %i", queue.transactions.count);
for (SKPaymentTransaction *transaction in queue.transactions)
{
if(SKPaymentTransactionStateRestored){
NSLog(#"Transaction state -> Restored");
//called when the user successfully restores a purchase
[self doRemoveAds];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
}
}
}
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions{
for(SKPaymentTransaction *transaction in transactions){
switch (transaction.transactionState){
case SKPaymentTransactionStatePurchasing: NSLog(#"Transaction state -> Purchasing");
//called when the user is in the process of purchasing, do not add any of your own code here.
break;
case SKPaymentTransactionStatePurchased:
//this is called when the user has successfully purchased the package (Cha-Ching!)
[self doRemoveAds]; //you can add your code for what you want to happen when the user buys the purchase here, for this tutorial we use removing ads
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
NSLog(#"Transaction state -> Purchased");
break;
case SKPaymentTransactionStateRestored:
NSLog(#"Transaction state -> Restored");
//add the same code as you did from SKPaymentTransactionStatePurchased here
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
case SKPaymentTransactionStateFailed:
//called when the transaction does not finnish
if(transaction.error.code != SKErrorPaymentCancelled){
NSLog(#"Transaction state -> Cancelled");
//the user cancelled the payment ;(
}
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
}
}
}
- (void)doRemoveAds{
NSLog(#"Bitches");
}
If it's a debug build you probably need to add a sandbox user on iTunes connect this will allow you to use IAP that have not been reviewed yet. Note this user should match your iTunes connect account.
An update of my existing app has been published on the app store today with in-app purchase to remove ads. I had to put a restore button as it is non-consumable in-app purchase. i have noticed that i can restore the in-app purchase without even purchasing it.
The app went for sale about 4 hours ago. Is this normal for a new app? does the app store take some time to hook up the in-app purchase (probably a silly question)? or have i done something wrong in my code?
Please response if you know whats going on.
EDIT:
Here is the code i used
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
for (SKPaymentTransaction *transaction in transactions)
{
switch (transaction.transactionState)
{
case SKPaymentTransactionStatePurchased:
[self completeTransaction:transaction];
break;
case SKPaymentTransactionStateRestored:
break;
case SKPaymentTransactionStateFailed:
[self failedTransaction:transaction];
break;
default:
break;
}
}
}
- (void) restoreTransaction: (SKPaymentTransaction *)transaction
{
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
[[SKPaymentQueue defaultQueue]addTransactionObserver:self];
}
-(void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue
{
if (SKPaymentTransactionStatePurchased)
{
//save purchase
NSUserDefaults *savePurchase = [NSUserDefaults standardUserDefaults];
[savePurchase setBool:TRUE forKey:k_Save];
[savePurchase synchronize];
_adBanner.hidden = YES;
// alert after successful restore.
UIAlertView *restoreTransactionAlert = [[UIAlertView alloc] initWithTitle:#"Congratulations!"
message:#"Your purchase is restored."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[restoreTransactionAlert show];
}
else
{
// alert after unsuccessful restore.
UIAlertView *restoreTransactionAlert = [[UIAlertView alloc] initWithTitle:#"Restore failed"
message:#"Restore failed. please try again"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[restoreTransactionAlert show];
}
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
}
- (void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error
{
}
Your implementation for restoring transactions is incorrect. Your condition in the -restoreTransactionsCompleted method, i.e.
if (SKPaymentTransactionStatePurchased)
Will always return true, as SKPaymentTransactionStatePurchased is an enum with value 1.
Look up on the documentation on restoring transactions here.