Check receipt of receipts in iOS 6 and 7

I check receipts in applications with the following code:

- (void) completeTransaction:(SKPaymentTransaction*) transaction
{   
    NSData* receipt = nil;

    // 1. Attempt <app receipt> first (iOS 7.x)

    NSBundle* mainBundle = [NSBundle mainBundle];

    if ([mainBundle respondsToSelector:@selector(appStoreReceiptURL)]) {

        NSURL* appStoreReceiptURL = [mainBundle appStoreReceiptURL]; // <- CRASH

        receipt = [NSData dataWithContentsOfURL:appStoreReceiptURL];
    }

    // 2. Fallback to <transaction receipt> (iOS < 7.0)
    if (!receipt) {    
        receipt = [transaction transactionReceipt];
    }

    // 3. Have server verify it with iTunes:    
    [self verifyReceipt:receipt forTransaction:transaction];
}

On an iOS 6 device, execution stops on the line NSURL* appStoreReceiptURL = [mainBundle appStoreReceiptURL];, and the console spits:

-[NSBundle appStoreReceiptURL]: unrecognized selector sent to instance 0x208492d0

Am I missing something? Shouldn't I -respondsToSelector:take care of this? Should I go back to checking the OS version directly?

+4
source share
1 answer

YES, you should check the version number directly in the case of this appStoreReceiptURL method. appStoreReceiptURL

On iOS, use the following code to determine if this method is available:

if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
   // Load resources for iOS 6.1 or earlier
} else {
   // Load resources for iOS 7 or later
}

. responsesToSelector: . iOS 7 (appStoreReceiptURL) API, doNotRecognizeSelector:.

: NSBundle Class

+8

All Articles