验证iOS 6和7中的购买收据

 rongrong_102077 发布于 2023-02-09 08:14

我正在使用以下代码验证应用内购买收据:

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

    // 1. Attempt  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  (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:应该照顾这个吗?我必须回头直接检查操作系统版本吗?

1 个回答
  • 是的,你应该在这个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类参考

    2023-02-09 08:20 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有