8
8
import SwiftUI
9
9
import StoreKit
10
10
11
- /// 管理用户的权限状态
12
- class EntitlementManager : ObservableObject {
13
- /// UserDefaults 实例,用于存储权限状态
14
- static let userDefaults = UserDefaults ( suiteName: " com.wangchujiang.InAppPurchaseManager.vip " ) !
15
- /// 使用 @AppStorage 将 hasPro 属性保存到 UserDefaults 中
16
- @AppStorage ( " hasPro " , store: userDefaults) var hasPro : Bool = false
17
- }
18
-
19
11
/// 管理订阅产品和购买记录
20
12
@MainActor class SubscriptionsManager : NSObject , ObservableObject {
21
13
/// 订阅产品的标识符数组
@@ -26,15 +18,16 @@ class EntitlementManager: ObservableObject {
26
18
]
27
19
/// 记录已购买的产品标识符集合
28
20
var purchasedProductIDs : Set < String > = [ ]
21
+ /// UserDefaults 实例,用于存储权限状态
22
+ static let userDefaults = UserDefaults ( suiteName: " com.wangchujiang.InAppPurchaseManager.vip " ) !
23
+ /// 使用 @AppStorage 将 hasPro 属性保存到 UserDefaults 中
24
+ @AppStorage ( " hasPro " , store: userDefaults) var hasPro : Bool = false
29
25
/// 发布订阅产品信息
30
26
@Published var products : [ Product ] = [ ]
31
- /// 授权管理器
32
- private var entitlementManager : EntitlementManager ? = nil
33
27
/// 更新任务
34
28
private var updates : Task < Void , Never > ? = nil
35
29
/// 初始化方法,接收 EntitlementManager 实例作为参数
36
- init ( entitlementManager: EntitlementManager ) {
37
- self . entitlementManager = entitlementManager
30
+ override init ( ) {
38
31
super. init ( )
39
32
// 监听交易更新
40
33
self . updates = observeTransactionUpdates ( )
@@ -115,21 +108,40 @@ extension SubscriptionsManager {
115
108
func updatePurchasedProducts( ) async {
116
109
/// 一系列最新交易,使用户有权进行应用内购买和订阅。
117
110
for await result in Transaction . currentEntitlements {
118
- guard case . verified( let transaction) = result else {
119
- continue
120
- }
121
- if transaction . revocationDate == nil {
122
- // 如果交易未被撤销,则将产品标识符添加到已购买集合中
123
- if ! self . purchasedProductIDs . contains ( transaction . productID ) {
124
- self . purchasedProductIDs . insert ( transaction . productID )
125
- }
126
- } else {
127
- // 如果交易被撤销,则从已购买集合中移除产品标识符
128
- self . purchasedProductIDs. remove ( transaction. productID)
111
+ guard case . verified( let transaction) = result else { continue }
112
+ handleTransaction ( transaction )
113
+ }
114
+ }
115
+ // MARK: - 处理交易
116
+ /// 处理交易
117
+ private func handleTransaction ( _ transaction : StoreKit . Transaction ) {
118
+ if transaction . revocationDate == nil {
119
+ // 如果交易未被撤销,则将产品标识符添加到已购买集合中
120
+ if ! self . purchasedProductIDs . contains ( transaction . productID ) {
121
+ self . purchasedProductIDs. insert ( transaction. productID)
129
122
}
123
+ } else {
124
+ // 如果交易被撤销,则从已购买集合中移除产品标识符
125
+ self . purchasedProductIDs. remove ( transaction. productID)
130
126
}
131
- // 更新 EntitlementManager 的 hasPro 属性
132
- self . entitlementManager? . hasPro = !self . purchasedProductIDs. isEmpty
127
+
128
+ // 通过 productID 获取 Product 对象
129
+ guard let product = products. first ( where: { $0. id == transaction. productID } ) else {
130
+ // 处理 product 不存在的情况
131
+ print ( " Product with ID \( transaction. productID) not found. " )
132
+ return
133
+ }
134
+
135
+ if let expirationDate = transaction. expirationDate, product. type == . autoRenewable {
136
+ // 更新 EntitlementManager 的 hasPro 属性
137
+ hasPro = !isExpirationDate( expirationDate: expirationDate)
138
+ }
139
+ }
140
+
141
+ // MARK: - 判断订阅是否过期
142
+ /// 判断订阅是否过期
143
+ func isExpirationDate( expirationDate: Date ) -> Bool {
144
+ return expirationDate > Date ( ) // 如果 expirationDate 在 currentDate 之后,返回 true 表示未过期
133
145
}
134
146
// MARK: - 恢复购买
135
147
/// 异步恢复购买
@@ -140,13 +152,13 @@ extension SubscriptionsManager {
140
152
// 更新已购买的产品
141
153
await updatePurchasedProducts ( )
142
154
} catch {
143
- print ( error)
155
+ print ( " Restore purchases failed: \( error. localizedDescription ) " )
144
156
}
145
157
}
146
158
}
147
159
148
160
// MARK: - SKPaymentTransactionObserver 实现
149
- extension SubscriptionsManager : SKPaymentTransactionObserver {
161
+ extension SubscriptionsManager : @ preconcurrency SKPaymentTransactionObserver {
150
162
// 支付队列更新交易
151
163
func paymentQueue( _ queue: SKPaymentQueue , updatedTransactions transactions: [ SKPaymentTransaction ] ) {
152
164
print ( " Subscriptions Payment Queue! updated! " )
0 commit comments