prabhu
prabhu

Reputation: 782

in app purchase in ios, EXC_BAD_ACCESS

I want to implement In-App-Purchase in my ios app, i have UPGRADE button, when user click on upgrade, I am sending product request using SKPayment SKProductsRequest, and added its delegate methods, the code is as shown below, whenever i click on upgrade button, after 2-3 seconds, it gives me error as 'executing bad access'. It giving error in sendRequest method only. Please give me the 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];
    }
}

Upvotes: 3

Views: 2225

Answers (3)

summer
summer

Reputation: 47

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

You could keep the memory by define it as a property in viewDidLoad

self.skPaymentQueue = [SKPaymentQueue defaultQueue];

Upvotes: 0

prabhu
prabhu

Reputation: 782

The problem was in my code, I not handled the object of SKPayment, so that before getting response from apple, my SKPayment object was released, so I got that bad access error, I declared the object globally and then its work fine. It was only my silly mistake, sorry for that.

Upvotes: 3

Steve Yung
Steve Yung

Reputation: 68

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

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

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

Upvotes: 0

Related Questions