Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

erdinc27

macrumors regular
Original poster
Jul 20, 2011
168
1
I try to put In App Purchase in application. I tested application in real device and everything worked well then i sent it to Apple to review and it was rejected. The reason is nothing happens when purchase button tapped. I didnt change any code and tested it again. Really nothing happens. Transaction Status always seen canceled. Here the codes i have.
Code:
- (IBAction)removeAdsTapped {
    
    if ([SKPaymentQueue canMakePayments]) {
       
        NSLog(@"User Can Make payment");
        SKProductsRequest *proRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:kRemoveAdsProductIdentifier]];
        proRequest.delegate = (id)self;
        [proRequest start];
        
    }
    else{
        NSLog(@"Cant make payment");
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"You can't make payment" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
    }
    
}

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
    
    SKProduct *validProduct = nil;
    NSUInteger 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");
    }
}

- (void) purchase:(SKProduct *)product{
    
    SKPayment *payment = [SKPayment paymentWithProduct:product];
    [[SKPaymentQueue defaultQueue] addTransactionObserver:(id)self];
    [[SKPaymentQueue defaultQueue] addPayment:payment];
}

- (void) paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue {
  
    NSLog(@"received restored transactions: %lu", (unsigned long)queue.transactions.count);
    
    for (SKPaymentTransaction *transaction in queue.transactions)
    {
        if(SKPaymentTransactionStateRestored){
            NSLog(@"Transaction state -> Restored");
            NSString *productID = transaction.payment.productIdentifier;
            
            [self.arrPurchasedItemIDs addObject:productID];
            [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
           
            break;
        }
        
    }
    
}

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions{
    
    for(SKPaymentTransaction *transaction in transactions){
        
        switch (transaction.transactionState){
                
            case SKPaymentTransactionStateDeferred:
                break;
                
            case SKPaymentTransactionStatePurchasing:
                NSLog(@"Transaction state -> Purchasing");
                break;
                
            case SKPaymentTransactionStatePurchased:
                
                [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"areAddsRemoved"];
                [[NSUserDefaults standardUserDefaults] synchronize];
                
                [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
               
                NSLog(@"Transaction state -> Purchased");
                break;
                
            case SKPaymentTransactionStateRestored:
                NSLog(@"Transaction state -> Restored");
                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
                break;
                
            case SKPaymentTransactionStateFailed:
                
                if(transaction.error.code != SKErrorPaymentCancelled){
                    NSLog(@"Transaction state -> Cancelled");
                }
                
                
                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
                break;
        }
    }
}
and in the log screen

Can Make payment
Products Available!
Transaction state -> Purchasing
Transaction state -> Cancelled

what can be problem here ?
 
Code:
if(transaction.error.code != SKErrorPaymentCancelled){
                    NSLog(@"Transaction state -> Cancelled");
                }

This is not showing the actual error, it could be any error BUT SKErrorPaymentCancelled
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.