跳转到正文

创建 iOS 订阅组

GitHub

订阅组对于在 iOS 应用中组织和管理多个订阅层级至关重要。理解其工作方式,是实现升级、降级和同级切换功能的关键。

订阅组是一组相关订阅,用户可在其中选择。用户同一时间只能订阅组内的一个订阅。当用户更换订阅时,Apple 会自动处理过渡。

订阅组可以实现:

  • 分层定价: 提供基础版、高级版和终极版方案
  • 不同的订阅周期: 月度、年度及终身选项
  • 升级/降级逻辑:自动处理订阅变更
  • 简化管理:将相关订阅归为一组

在组内,每个订阅应按价值从高到低排序(等级 1 为最高)。此排序决定订阅变更的分类方式:

订阅组层级

等级 1 (最高价值)

  • 高级年度版($99.99/年)
  • 终极月度版($19.99/月)

第 2 级 (中等价值)

  • 标准年费($49.99/年)
  • 高级月费($9.99/月)

第 3 级 (最低价值)

  • 基础年费($29.99/年)
  • 标准月费($4.99/月)

Apple 会根据等级排序自动处理三类订阅变更:

切换至 更高等级 订阅(例如,等级 2 → 等级 1)。

行为:

  • 立即 生效
  • 用户将获得 按比例退款 剩余时间
  • 新订阅立即生效

示例:

// User currently has: Standard Monthly (Level 2)
// User upgrades to: Premium Annual (Level 1)
// Result: Immediate access to Premium, refund for unused Standard time

切换至 较低档位 的订阅(例如:等级 1 → 等级 2)。

行为:

  • 在下次 续订日期生效
  • 用户保留当前订阅直至本周期结束
  • 新订阅会在到期后自动开始

示例:

// User currently has: Premium Annual (Level 1)
// User downgrades to: Standard Monthly (Level 2)
// Result: Premium access continues until annual renewal date, then switches

切换到其他订阅 同一等级.

具体行为取决于时长:

不同时长 → 表现类似于 降级

  • 在下次续订日期生效
  • 示例:月度高级版(等级 1)→ 年度高级版(等级 1)

时长相同 → 表现类似于 升级

  • 立即生效
  • 示例:Premium Monthly (Level 1) → Ultimate Monthly (Level 1)
  1. 导航到“订阅”

    在 App Store Connect 中,选择您的 App 并前往 盈利 > 订阅.

  2. 创建组

    点击 + 点击“Subscription Groups”旁的按钮创建新组。

  3. 命名分组

    选择一个能体现所含订阅的描述性名称:

    • “高级访问”
    • “云存储套餐”
    • “高级功能”
  4. 添加订阅

    创建分组后,向其中添加各个订阅。每个订阅都会有一个等级排名。

  5. 设置等级排名

    按价值从高(1)到低排列订阅。请考虑:

    • 年度套餐通常比月度套餐排名更高
    • 价格较高的层级排在价格较低的层级之上
    • Ultimate/高级层级排名最高

native-purchases 插件会自动处理订阅组逻辑:

import { NativePurchases, PURCHASE_TYPE } from '@capgo/native-purchases';
// Fetch all subscriptions in a group
const { products } = await NativePurchases.getProducts({
productIdentifiers: ['premium_monthly', 'premium_annual', 'ultimate_monthly'],
productType: PURCHASE_TYPE.SUBS,
});
// Display current subscription using StoreKit transactions
const { purchases } = await NativePurchases.getPurchases({
productType: PURCHASE_TYPE.SUBS,
});
const activeSubs = purchases.filter((purchase) => purchase.isActive);
// Detect pending downgrade/cancellation (StoreKit sets willCancel === true)
const pendingChange = purchases.find((purchase) => purchase.willCancel === true);
if (pendingChange) {
console.log('Subscription will stop auto-renewing on', pendingChange.expirationDate);
}
// Purchase (StoreKit handles upgrades/downgrades automatically)
await NativePurchases.purchaseProduct({
productIdentifier: 'premium_annual',
productType: PURCHASE_TYPE.SUBS,
});
// Listen for StoreKit updates (fires on upgrades/downgrades/refunds)
NativePurchases.addListener('transactionUpdated', (transaction) => {
console.log('Subscription updated:', transaction);
});
import { NativePurchases, PURCHASE_TYPE } from '@capgo/native-purchases';
// Get current subscription info
const { purchases } = await NativePurchases.getPurchases({
productType: PURCHASE_TYPE.SUBS,
});
const currentSubscription = purchases.find(
(purchase) => purchase.subscriptionState === 'subscribed',
);
if (currentSubscription) {
// StoreKit reports if user cancelled auto-renew
if (currentSubscription.willCancel) {
console.log(
`User cancelled. Access remains until ${currentSubscription.expirationDate}`,
);
}
if (currentSubscription.isUpgraded) {
console.log('User recently upgraded to this plan.');
}
}
// Listen for automatic upgrades/downgrades
NativePurchases.addListener('transactionUpdated', (transaction) => {
console.log('Subscription changed!', transaction);
if (transaction.subscriptionState === 'revoked') {
revokeAccess();
} else if (transaction.isActive) {
unlockPremiumFeatures();
}
});

始终清晰说明变更后的行为:

对于升级:

“您将立即获得 Premium 功能。我们将按比例折算您当前的订阅费用。”

对于降级:

“您将继续享有 Premium 权限,直到 [renewal date],之后切换到 Standard。”

对于同级变更:

“您的方案将在下次续订([date])时改为按年计费。”

使用 Apple 的 App Store Server Notifications v2 或您自己的收据验证后端,将 StoreKit 的变更同步到您的数据库中。将服务器通知与 transactionUpdated listener 配合使用,以确保客户端和后端保持同步。

  • 将相关订阅放在同一组中
  • 不要混合不相关的功能(例如,存储和去广告)
  • 为不同的功能集创建单独的组
  • 年度计划 → 比月度计划等级更高(同一层级)
  • 价格更高的层级 → 等级更高
  • 考虑价值,而不仅仅是价格
  • 清晰展示当前订阅
  • 显示组内所有可用选项
  • 标明哪些变更立即生效、哪些续期时生效
  • 支持轻松切换套餐
  • 测试所有升级场景
  • 测试所有降级场景
  • 验证跨级变更行为
  • 检查 webhook 触发
Level 1: Ultimate Monthly ($19.99)
Level 2: Premium Monthly ($9.99)
Level 3: Basic Monthly ($4.99)
  • Basic → Premium:升级(立即生效)
  • Premium → Ultimate:升级(立即生效)
  • Ultimate → Premium:降级(续费时生效)
  • Basic → Ultimate:升级(立即生效)
Level 1: Premium Annual ($99.99/year)
Level 2: Premium Monthly ($9.99/month)
  • 月付 → 年付:跨级升级(续费时)
  • 年付 → 月付:降级(续订时生效)

场景 3:多层级、多时长订阅

标题为“场景 3:多层级多时长”的章节
Level 1: Ultimate Annual ($199/year)
Level 2: Ultimate Monthly ($19.99/month)
Level 3: Premium Annual ($99/year)
Level 4: Premium Monthly ($9.99/month)
Level 5: Basic Annual ($49/year)
Level 6: Basic Monthly ($4.99/month)

此配置在保持清晰的升降级逻辑的同时,提供了最大的灵活性。

订阅未出现在分组中:

  • 确认它已分配到正确的分组
  • 检查它是否至少处于“Ready to Submit”状态
  • 确保 Product ID 正确

升降级行为异常:

  • 检查等级排名(1 = 最高)
  • 确认订阅层级是否合理
  • 检查等级设置是否正确

来自不同组的产品:

  • 用户可以同时订阅多个组
  • 这是有意为之 - 请将相关产品放在同一组

getActiveProducts 显示多个订阅:

  • 检查订阅是否属于不同组
  • 确认用户不是通过“家人共享”订阅的
  • 在 App Store Connect 中检查订阅状态

欲了解更多详情,请参阅 Apple 官方关于订阅组的文档.

从“创建 iOS 订阅组”继续

“从创建 iOS 订阅组继续”部分

如果你正在使用 创建 iOS 订阅组 要规划商店审核与分发,请将其与 使用 @capgo/native-purchases 关于“使用 @capgo/native-purchases”中的原生能力, @capgo/capacitor-in-app-review 有关 @capgo/capacitor-in-app-review 中的实现细节, 使用 @capgo/capacitor-in-app-review 有关《使用 @capgo/capacitor-in-app-review》中的原生能力, @capgo/capacitor-native-market 有关 @capgo/capacitor-native-market 的实现细节,以及 使用 @capgo/capacitor-native-market 有关使用 @capgo/capacitor-native-market. 时的原生能力