iOS端内购订阅请求失败,提示未知错误求助
iOS内购订阅请求返回未知错误的排查方案
问题场景
已成功从App Store获取产品信息(日志如下),但调用requestSubscription发起订阅时返回{"error": [Error: An unknown error occurred], "success": false},Xcode相关配置已完成。
获取到的产品信息:
[{"countryCode": "PAK", "currency": "PKR", "description": "Buy to add amazing features ", "discounts": [], "introductoryPrice": "", "introductoryPriceAsAmountIOS": "", "introductoryPriceNumberOfPeriodsIOS": "", "introductoryPricePaymentModeIOS": "", "introductoryPriceSubscriptionPeriodIOS": "", "localizedPrice": "Rs 12,900", "platform": "ios", "price": "12900", "productId": "Feast_179", "subscriptionPeriodNumberIOS": "1", "subscriptionPeriodUnitIOS": "MONTH", "title": "Feast Premium access", "type": "subs"}]
请求代码:
requestSubscription({ sku: myProducts, ...('offerToken' && { subscriptionOffers: [ { sku: myProducts, // as a string offerToken: Platform.OS == 'android' ? value[0].subscriptionOfferDetails[0].offerToken : 'offerToken', }, ], }), })
核心问题排查与修复
- sku参数格式错误:
requestSubscription的sku需传入单个产品ID字符串,而非数组。从日志看正确的productId是Feast_179,需将myProducts替换为该字符串。 - iOS端offerToken硬编码无效:当前产品无优惠(日志中
introductoryPrice为空、discounts数组为空),不应传入subscriptionOffers参数。硬填'offerToken'会直接导致请求失败。若后续有优惠,需从产品对应字段提取有效令牌,而非硬编码。 - 条件判断逻辑错误:
'offerToken' && {...}的判断永远为真,会强制添加无效的subscriptionOffers。应改为仅在Android端有有效优惠令牌时才传入该参数。
修复后的示例代码
const productId = 'Feast_179'; requestSubscription({ sku: productId, ...(Platform.OS === 'android' && value[0]?.subscriptionOfferDetails?.length > 0 ? { subscriptionOffers: [ { sku: productId, offerToken: value[0].subscriptionOfferDetails[0].offerToken, }, ], } : {}), })
额外排查点
- 确认测试设备已登录沙箱测试账号,而非正式Apple ID。
- 检查产品ID
Feast_179与App Store Connect中配置的完全一致,大小写、拼写无错误。 - 确保订阅类型在App Store Connect中已正确配置,状态为“准备提交”或“已批准”。
- 查看Xcode控制台的详细错误日志,可能会有比“未知错误”更具体的信息(如权限、网络或令牌无效等)。
内容的提问来源于stack exchange,提问作者Sabahat rathore
相关产品推荐
相关产品推荐

