我正在使用以下代码验证应用内购买收据:
- (void) completeTransaction:(SKPaymentTransaction*) transaction { NSData* receipt = nil; // 1. Attemptfirst (iOS 7.x) NSBundle* mainBundle = [NSBundle mainBundle]; if ([mainBundle respondsToSelector:@selector(appStoreReceiptURL)]) { NSURL* appStoreReceiptURL = [mainBundle appStoreReceiptURL]; // <- CRASH receipt = [NSData dataWithContentsOfURL:appStoreReceiptURL]; } // 2. Fallback to (iOS < 7.0) if (!receipt) { receipt = [transaction transactionReceipt]; } // 3. Have server verify it with iTunes: [self verifyReceipt:receipt forTransaction:transaction]; }
在iOS 6设备上,执行在该行停止NSURL* appStoreReceiptURL = [mainBundle appStoreReceiptURL];
并且控制台吐出:
-[NSBundle appStoreReceiptURL]: unrecognized selector sent to instance 0x208492d0
我错过了什么吗?不-respondsToSelector:
应该照顾这个吗?我必须回头直接检查操作系统版本吗?
是的,你应该在这个appStoreReceiptURL方法的情况下直接检查版本号. appStoreReceiptURL
在iOS中,使用以下代码检测此方法是否可用:
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) { // Load resources for iOS 6.1 or earlier } else { // Load resources for iOS 7 or later }
注意:这里不能使用使用respondsToSelector:方法进行弱链接的一般最佳实践.在iOS 7之前,方法(appStoreReceiptURL)是作为私有API实现的,但该实现称为doesNotRecognizeSelector:方法.
参考:NSBundle类参考