In-app purchases

I have In-App Purchases in my app, these are just one-time purchases, no purchases. They totally work and the implementation even lives on in the app store. However, there seem to be random times when a user tries to buy an answer from Apple, EXTREMELY SLOW, for example, on the order of 30+ seconds over Wi-Fi over 100 Mbps or over LTE / 3G. It looks like it has nothing to do with connectivity, it just takes a while for Apple to respond.

Here's the basic code that creates this invitation and processes the response:

// User has decided to buy a sticker pack
- (void) promptToBuyPack:(StickerPack *)pack {
    if([SKPaymentQueue canMakePayments]) {
        if([products valueForKey:pack.packID]) {
            SKPayment *payment = [SKPayment paymentWithProduct:[products valueForKey:pack.packID]];
            [[SKPaymentQueue defaultQueue] addPayment:payment];
        }
        else {
            nlog(@"User attempted to buy a pack that iTunes does not recognize");
        }

    }
    else {
        UIAlertView *a = [[UIAlertView alloc] initWithTitle:@"Can not make purchases" message:@"Your device does not have purchases enabled. Please enable them in order to proceed" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [a show];
    }
}



- (void) grantProductAccess:(NSString *)productIdentifier {
    // Store purchase in user defaults
    nlog(@"Bought product: %@", productIdentifier);
    [[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithBool:YES] forKey:productIdentifier];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (void) tellUserPurchaseIsComplete {
    UIAlertView *a = [[UIAlertView alloc] initWithTitle:@"Purchase Complete" message:@"Your pack is now unlocked, Enjoy!" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    [a show];
    [a release];
}

- (void) paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
    for(SKPaymentTransaction *transaction in transactions) {
        BOOL canFinishTransaction = NO;
        switch (transaction.transactionState) {
            case SKPaymentTransactionStatePurchased:
                canFinishTransaction = YES;
                [self grantProductAccess:transaction.payment.productIdentifier];
                [self tellUserPurchaseIsComplete];
                if(iPad)
                    [FlurryAPI logEvent:[NSString stringWithFormat:@"Purchased %@ on iPad",transaction.payment.productIdentifier]];
                else
                    [FlurryAPI logEvent:[NSString stringWithFormat:@"Purchased %@ on iPod",transaction.payment.productIdentifier]];
                break;
            case SKPaymentTransactionStateFailed:
                nlog(@"Transactin failed");
                canFinishTransaction = YES;
                break;
            case SKPaymentTransactionStateRestored:
                nlog(@"SKPaymentTransactionStateRestored");
                canFinishTransaction = YES;
                [self grantProductAccess:transaction.originalTransaction.payment.productIdentifier];
                break;
            default:
                nlog(@"SKPaymentTransactionDefault");
                break;
        }

        // Remove from payment queue
        if(canFinishTransaction)
            [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
    }
}

      

+3


source to share





All Articles