StoreKit2疑似Bug:已不存在的订阅仍被添加至订阅列表
StoreKit2 疑难:已下架订阅仍出现在当前订阅列表,是代码问题还是Apple Bug?
实际已不存在的订阅,代码仍会把它加入当前订阅列表(见截图)。
// update the customers products @MainActor func updateCustomerProductStatus() async { var purchasedSubs: [Product] = [] var purchasedIAP: [Product] = [] //iterate through all the user's purchased products for await result in Transaction.currentEntitlements { do { //again check if transaction is verified let transaction = try checkVerified(result) //Check the `productType` of the transaction and get the corresponding product from the store. switch transaction.productType { case .consumable: if let iap = iaps.first(where: { $0.id == transaction.productID }) { purchasedIAP.append(iap) } case .autoRenewable: if let subscription = subscriptions.first(where: { $0.id == transaction.productID }) { //SUBSCRIPTION DOESN'T EXIST AND STILL GETS APPENDED!! purchasedSubs.append(subscription) } default: break } } catch { //storekit has a transaction that fails verification, don't delvier content to the user print("Transaction failed verification") } //finally assign the purchased products self.purchasedIAPs = purchasedIAP self.purchasedSubscriptions = purchasedSubs } }
这会不会只是Xcode调试环境下的StoreKit错误?就像其他只在调试中出现、生产环境正常的问题一样?求助!我不想把1%的营收交给RevenueCat!
排查方向与解决方案
调试环境缓存问题
Sandbox测试环境经常会保留旧的交易缓存,尤其是测试账号的订阅状态可能没有及时同步。可以试试:- 去App Store Connect的「沙盒测试员」页面,重置对应测试账号的订阅状态
- 卸载App后重装,清除本地StoreKit缓存
- 重启Xcode和测试设备/模拟器
本地
subscriptions数组未更新
代码是从本地维护的subscriptions数组中匹配productID,如果这个数组没同步移除已下架的订阅ID,就会出现匹配成功的情况。先检查你的产品数据源是否已经过滤掉了下架的订阅。缺少订阅有效性判断
当前代码只匹配了productID,但没验证订阅是否还处于有效期。即使订阅已下架,Sandbox中可能仍存在未过期的交易记录,需要加上过期时间判断:case .autoRenewable: if let subscription = subscriptions.first(where: { $0.id == transaction.productID }), // 只保留未过期的订阅 transaction.expirationDate == nil || transaction.expirationDate! > Date() { purchasedSubs.append(subscription) }Sandbox环境特性
沙盒的订阅行为和生产环境有差异,比如取消订阅后可能仍会显示一段时间的有效状态,这是测试环境的设计特性。建议用TestFlight的生产测试版本验证,看问题是否复现。
内容的提问来源于stack exchange,提问作者Nat Serrano
相关产品推荐
相关产品推荐

