how to work on In-App-Purchase in ios objective c?

301 Views Asked by At

I am trying to add In-App-Purchare in application. With the help of some YouTube videos and other tutorials I have performed some code in my project. But there is no response on tapping the button. Here is my code which i have integrated in my project. Please help me to complete this task

-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {

NSUInteger count = [response.products count];
if(count > 0){
    _product = [response.products objectAtIndex:0];
    NSLog(@"Products Available!");
    [self purchase:_product];
}
else if(!_product){
    NSLog(@"No products available");

}
}

- (void)purchase:(SKProduct *)product{
    SKPayment *payment = [SKPayment paymentWithProduct:product];

    [[SKPaymentQueue defaultQueue] addTransactionObserver: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(transaction.transactionState == SKPaymentTransactionStateRestored){

            NSLog(@"Transaction state -> Restored");

            [self buyBasicEdition];
            [[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");

                break;
            case SKPaymentTransactionStatePurchased:

                [self buyBasicEdition];
                [[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;
        }
    }
}

-(void)buyBasicEdition {

    [[NSUserDefaults standardUserDefaults]setObject:@"low" forKey:@"points"];
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    SelectTeamVC *viewVc = [storyBoard instantiateViewControllerWithIdentifier:@"SelectTeamVC"];
    [self.navigationController  pushViewController:viewVc animated:YES];
}

Here is the button which I tap to initialise the payment process

- (IBAction)edition1:(id)sender {

    if([SKPaymentQueue canMakePayments]){
        NSLog(@"User can make payments");

        SKProductsRequest *productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:_productId]];
        productsRequest.delegate = self;
        [productsRequest start];

    }
    else{
        NSLog(@"User cannot make payments due to parental controls");

    }

}

EDIT: The logs I am seeing on console

Taboo[1649:358442] User can make payments 2018-08-03 18:23:10.954499+0530 Taboo[1649:358442] Products Available! 2018-08-03 18:23:10.956229+0530 Taboo[1649:358442] Transaction state -> Purchasing

0

There are 0 best solutions below