0

When trying to purchase with the iAP the app crashes and shows in Xcode. I bought this code from someone but nobody else complained of an error with iAP on the comments page and I could swear I tested and it worked fine before. I even tried the project fresh and it still is crashing. I'm only a beginner at coding as well so I can do basic things but please be specific Id appreciate it. I don't think it has anything to do with provisions profiles or certificates cause its right in the code. Also its been a while and might sound dumb, but does everything need to be configured in iTunes connect with the same bundle ID and iAP info to work? Ive tried it with everything setup and it still don't work. Regardless, code shouldn't crash. Please take a look, I'll post the screenshots and code.

screen of error

Most the code from my AppController.mm that has to do with iAP is Here: (Sorry, not sure why it is piecing it weird in code format...I just copied and pasted. So I'm only going to put the error parts)

    - (void) openRestore:(NSObject*)prms
{

    [[SKPaymentQueue defaultQueue]restoreCompletedTransactions];

}



- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];

    NSArray *myProduct = response.products;
    NSLog(@"%@",[[myProduct objectAtIndex:0] productIdentifier]);

    SKPayment *newPayment = [SKPayment paymentWithProduct:[myProduct objectAtIndex:0]];
    [[SKPaymentQueue defaultQueue] addPayment:newPayment];


}

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
    for (SKPaymentTransaction *transaction in transactions)
    {
        switch (transaction.transactionState)
        {
            case SKPaymentTransactionStatePurchased:
                [self completeTransaction:transaction];
                break;
            case SKPaymentTransactionStateFailed:
                [self failedTransaction:transaction];
                break;
            case SKPaymentTransactionStateRestored:
                [self restoreTransaction:transaction];
            default:
                break;
        }
    }
}
Zach
  • 41
  • 4
  • When you edit your post in the browser, highlight the entire code part and look for the `Code Sample` button in the toolbar above the text editing area. It looks like a set of curly braces (`{ }`). – Gamma Oct 25 '15 at 00:46

1 Answers1

0

According to the screenshot you posted, the following happens: your app requests a list of products from the server. The server responds by saying that there are no products (i.e. an empty array). The app then tries to print info about the first product in the array to the console (i.e. the product at index "0"). Since there is no index 0 in an empty array, it crashes with an index out of bounds exception.

Because your code is strongly expecting that there is always at least one product returned (which does not seem like a great design choice, but that's another point), it seems very likely that the immediate cause behind this particular crash is that iAPs are not set up correctly for this app. Your iAP handling code is not seeing what it is expecting to see.

I suggest referring to documentation how to create and test In-App Purchase Products for you app.

Edit (to answer Zach's request in the comments):

Well, making the app not crash in this particular spot is easy, but like I said, that's not going to magically make the In-App Purchase itself work if it's not set up correctly.

In order to prevent the crash in the code you posted, you can add an explicit check that the array of products is not empty, like so:

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];

    NSArray *myProduct = response.products;

    if (myProduct.count > 0) { 
        NSLog(@"%@",[[myProduct objectAtIndex:0] productIdentifier]);

        SKPayment *newPayment = [SKPayment paymentWithProduct:[myProduct objectAtIndex:0]];
        [[SKPaymentQueue defaultQueue] addPayment:newPayment];
    }
}
Gamma
  • 1,347
  • 11
  • 18
  • Thanks for your response. Well as I said I bought this code and nobody else complained of iAP not working. Could it be anything at all that I would only be doing differently? And could you please perhaps give me a work around fix of sorts. Apple's documentation is probably like ground zero for coding this and too much for a novice coder like me to even understand or get anything out of. Everything for iAP is in place, something just needs to be fixed. Is there anything I can do as far as fixing or adding a line of code? Thanks – Zach Oct 25 '15 at 03:21
  • Also could this: http://stackoverflow.com/questions/16279150/app-crashing-when-buying-in-app-purchase or: http://stackoverflow.com/questions/25734550/ios-in-app-purchase-related-crash help at all? If so where do i put these lines of code at? – Zach Oct 25 '15 at 03:22
  • I edited the answer with some code that should work around the crash. But the code in the app is just one side of the coin. The point still stands: you'll probably need to look into how to configure iAPs in itunes connect and your app, and how to test them in non-appstore builds in order to make this work for you. – Gamma Oct 25 '15 at 10:22