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编码的收据数据,解析后统计交易次数:
- 从
customerInfo?.latestReceipt拿到收据的base64字符串 - 解码后解析苹果收据的JSON格式(可使用
SwiftyStoreKit等库简化解析) - 统计收据中
in_app数组的元素数量,即为历史交易次数
不过更推荐使用第一种方案解决资格检测问题,而非自行统计收据数量,避免因苹果收据规则变动导致判断出错。
内容的提问来源于stack exchange,提问作者Jeff 974
相关产品推荐
相关产品推荐

