You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

RevenueCat与StoreKit:免费试用资格判断报错及试用状态检测问题

RevenueCat订阅试用资格检测与收据问题解决方案

1. 沙盒环境下checkTrialOrIntroDiscountEligibility报错处理

你遇到的**🍎‼️ App running on sandbox without a receipt file. Unable to determine into eligibility unless you've purchased before and there is a receipt available.**错误,是因为沙盒首次启动时系统未生成收据文件,导致RevenueCat无法验证试用资格。解决方法:

  • 生成空收据(无需恢复订阅):发起一次模拟购买后取消,触发系统生成收据:
    调用Purchases.shared.purchasePackage发起购买,在支付弹窗点击取消,之后再调用资格检测接口即可正常返回结果。
  • 手动刷新收据:结合SKReceiptRefreshRequest刷新收据后再检测资格,代码示例:
import StoreKit

func refreshReceiptAndCheckEligibility() {
    let receiptRequest = SKReceiptRefreshRequest(receiptProperties: nil)
    receiptRequest.delegate = self
    receiptRequest.start()
}

// 实现SKRequestDelegate协议
func requestDidFinish(_ request: SKRequest) {
    Purchases.shared.getOfferings { offerings, error in
        guard let product = offerings?.current?.availablePackages.first?.storeProduct else { return }
        Purchases.shared.checkTrialOrIntroDiscountEligibility(product: product) { eligibility in
            self.freeTrialElgibility = eligibility == .eligible
        }
    }
}

2. 正确判断用户是否处于免费试用阶段

你对periodType的理解有误,PeriodType枚举的正确对应关系:

  • .trial:当前处于免费试用阶段,rawValue为1
  • .intro:当前处于 introductory 优惠阶段,rawValue为2
  • .normal:当前处于正常订阅阶段,rawValue为0

正确的判断代码:

Purchases.shared.getCustomerInfo { customerInfo, error in
    self.isSubcriptionActive = customerInfo?.entitlements.all["All Access"]?.isActive == true
    // 直接判断枚举值,避免rawValue出错
    self.isTrialPeriod = customerInfo?.entitlements.all["All Access"]?.periodType == .trial
}

3. 获取收据相关信息的方法

RevenueCat没有直接提供“收据数量”的API,但可以通过CustomerInfo的latestReceipt字段获取base64编码的收据数据,解析后统计交易次数:

  1. 从customerInfo?.latestReceipt拿到收据的base64字符串
  2. 解码后解析苹果收据的JSON格式(可使用SwiftyStoreKit等库简化解析)
  3. 统计收据中in_app数组的元素数量,即为历史交易次数

不过更推荐使用第一种方案解决资格检测问题,而非自行统计收据数量,避免因苹果收据规则变动导致判断出错。

内容的提问来源于stack exchange,提问作者Jeff 974

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 00:53:20