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

macOS平台SwiftUI中如何通过SubscriptionStoreView取消订阅?

如何在macOS上通过SubscriptionStoreView实现取消订阅

问题背景

目标:使用SubscriptionStoreView完成订阅取消操作。
遇到的核心问题:

  1. .manageSubscriptionsSheet修饰符仅适配UIKit生态(iOS/iPadOS),macOS不支持该API。
  2. 原生SubscriptionStoreView未提供直接的取消订阅按钮。

解决方案

在macOS上,我们需要结合自定义交互与StoreKit系统API来实现订阅取消功能,具体实现步骤如下:

1. 先获取用户当前订阅状态(可选但推荐)

通过StoreKit API获取用户活跃订阅信息,判断是否存在可取消的订阅:

import StoreKit

func fetchActiveSubscriptions() async -> [Product.SubscriptionInfo] {
    do {
        // 替换为你的订阅产品ID数组
        let subscriptionInfo = try await Product.subscriptionInfo(for: ["com.yourapp.pro"])
        return subscriptionInfo.activeSubscriptions
    } catch {
        print("获取订阅信息失败:\(error.localizedDescription)")
        return []
    }
}

2. 实现订阅管理跳转逻辑

macOS上可通过StoreKit的系统API直接打开订阅管理界面(支持取消操作),兼容低版本的方案则引导用户打开系统设置:

import StoreKit

func openSubscriptionManagement() {
    Task {
        do {
            // macOS 12.0+、iOS 15.0+ 支持的API,直接打开系统订阅管理页
            try await AppStore.showManageSubscriptions()
        } catch {
            // 降级方案:打开系统设置
            if let settingsURL = URL(string: "app-settings:") {
                NSWorkspace.shared.open(settingsURL)
            }
        }
    }
}

3. 整合到SwiftUI视图中

修改原有视图,添加自定义的订阅管理按钮,同时保留SubscriptionStoreView用于展示订阅选项:

import SwiftUI
import StoreKit

struct ContentView: View {
    @State private var showSubscriptionSheet = false
    @State private var presentingiOSManageSheet = false

    var body: some View {
        VStack(spacing: 20) {
            Button("展示订阅选项") {
                showSubscriptionSheet = true
            }
            .sheet(isPresented: $showSubscriptionSheet) {
                SubscriptionStoreView(groupID: "21438756")
            }

            Button("管理订阅(可取消)") {
                openSubscriptionManagement()
            }
            .buttonStyle(.borderedProminent)
        }
        .padding()
        // iOS端保留原有专属的订阅管理Sheet
        #if os(iOS)
        .manageSubscriptionsSheet(
            isPresented: $presentingiOSManageSheet,
            subscriptionGroupID: "21438486"
        )
        #endif
    }

    private func openSubscriptionManagement() {
        Task {
            do {
                try await AppStore.showManageSubscriptions()
            } catch {
                if let settingsURL = URL(string: "app-settings:") {
                    NSWorkspace.shared.open(settingsURL)
                }
            }
        }
    }
}

关键说明

  • AppStore.showManageSubscriptions()是苹果提供的系统级API,打开的界面支持完整的订阅管理操作(包括取消),是macOS上的最优方案。
  • SubscriptionStoreView的设计定位是展示与购买订阅,本身不包含取消逻辑,这是苹果的统一设计规范,因此必须通过系统级入口完成取消操作。

内容的提问来源于stack exchange,提问作者Mane Manero

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 17:05:13