In app purchase in ios, EXC_BAD_ACCESS

I want to implement In-App-Purchase in my ios app, I have an UPGRADE button, when the user clicks on update, I send a product request using SKPayment SKProductsRequest and add its delegation methods, the code looks like below whenever, when i click refresh button after 2-3 seconds it gives me error like "performing bad access". This only gives an error in the sendRequest method. Please give me a solution.

-(void)sendRequest
{
    if ([SKPaymentQueue canMakePayments])
    {

        SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:@"thisIsMyProdID"]];
        request.delegate = self;
        [request start];
    }
}
-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
    for (SKPaymentTransaction *transaction in transactions)
    {
        switch (transaction.transactionState)
        {

            case SKPaymentTransactionStatePurchasing:
                NSLog(@"Processing...");
                break;

            case SKPaymentTransactionStatePurchased:
            {
                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
                //statusLabel.text = @"Done!";
                UIAlertView *tmp = [[UIAlertView alloc]
                                    initWithTitle:@"Complete"
                                    message:@"You have unlocked Feature 2!"
                                    delegate:self
                                    cancelButtonTitle:nil
                                    otherButtonTitles:@"Ok", nil];
                [tmp show];


            }
                               break;

            case SKPaymentTransactionStateRestored:
            {
                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
                // remove wait view here
            }
                break;

            case SKPaymentTransactionStateFailed:
            {
                if (transaction.error.code != SKErrorPaymentCancelled) {
                    NSLog(@"Error payment cancelled");
                }
                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
                // remove wait view here

            }
                break;

            default:
                break;
        }
    }
}

-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
    NSArray *products = response.products;
    if (products.count != 0)
    {
        product = products[0];

        SKPayment *payment = [SKPayment paymentWithProduct:product];

        [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
        [[SKPaymentQueue defaultQueue] addPayment:payment];

    }
    else
    {
        UIAlertView *tmp = [[UIAlertView alloc]
                            initWithTitle:@"Not Available"
                            message:@"No products to purchase"
                            delegate:self
                            cancelButtonTitle:nil
                            otherButtonTitles:@"Ok", nil];
        [tmp show];
    }
}

      

+3


source to share


3 answers


The problem was in my code, I didn't process the SKPayment object, so before receiving the response from apple, my SKPayment object was released, so I got this bad access error, I declared the object globally and then it worked fine. It was just my stupid mistake, sorry about that.



+3


source


Since SKProductRequest is a subclass of SKRequest, you will want to implement a delegate

  • (void) request: (SKRequest *) request didFailWithError: (NSError *) error


to get any errors this will probably point you in the right place. A good example is in a tutorial from raywenderlich.com ( http://www.raywenderlich.com/21081/introduction-to-in-app-purchases-in-ios-6-tutorial ).

0


source


@property (strong, nonatomic) SKProductsRequest *request;
@property (strong, nonatomic) SKPaymentQueue *skPaymentQueue;

      

You can save memory by defining it as property in viewDidLoad

self.skPaymentQueue = [SKPaymentQueue defaultQueue];

      

0


source







All Articles