我正在尝试在我的项目中实现 StoreKit,特别是自动更新订阅,使用Apple 的实现 StoreSit2 示例。
我重构了 Apple 的项目,仅根据我的应用程序需要进行年度订阅。我在 Github 上上传了重构的项目。我完全按照项目设置进行了所有操作,但是,我在文件中收到此错误:Store 和 StatusInfoView 位于以下行: 在“Store”第 214 行:
subscriptionGroupStatus = try? await subscriptions.first?.subscription?.status.first?.state
错误:
无法分配“Product.SubscriptionInfo.RenewalState??”类型的值 输入“RenewalState?” (又名 ‘可选
’)
在“StatusInfoView”第 134 行:
subscriptionStatus = try? await product.subscription?.status.last
错误:
无法分配“Product.SubscriptionInfo.Status??”类型的值到 输入“Product.SubscriptionInfo.Status?”
  Github 上的项目不会产生错误,它在我将文件复制粘贴到我的 Xcode 项目后显示。
我不知道如何修复值分配。
任何帮助将不胜感激
@MainActor
func updateCustomerProductStatus() async {
var purchasedSubscriptions: [Product] = []
//Iterate through all of the user's purchased products.
for await result in Transaction.currentEntitlements {
do {
//Check whether the transaction is verified. If it isn’t, catch `failedVerification` error.
let transaction = try checkVerified(result)
//Check the `productType` of the transaction and get the corresponding product from the store.
switch transaction.productType {
case .autoRenewable:
if let subscription = subscriptions.first(where: { $0.id == transaction.productID }) {
purchasedSubscriptions.append(subscription)
}
default:
break
}
} catch {
print()
}
}
//Update the store information with auto-renewable subscription products.
self.purchasedSubscriptions = purchasedSubscriptions
//Check the `subscriptionGroupStatus` to learn the auto-renewable subscription state to determine whether the customer
//is new (never subscribed), active, or inactive (expired subscription). This app has only one subscription
//group, so products in the subscriptions array all belong to the same group. The statuses that
//`product.subscription.status` returns apply to the entire subscription group.
//let storeProducts = try await Product.products(for: productIdToEmoji.keys)
subscriptionGroupStatus = try? await subscriptions.first?.subscription?.status.first?.state
}
struct StatusInfoView_Previews: PreviewProvider {
@StateObject static var store = Store()
@State static var subscription: Product?
@State static var subscriptionStatus: Product.SubscriptionInfo.Status?
static var previews: some View {
Group {
if let subscription, let subscriptionStatus {
StatusInfoView(product: subscription, status: subscriptionStatus)
.environmentObject(store)
}
}
.task {
guard let product = store.purchasedSubscriptions.first else {
return
}
subscription = product
subscriptionStatus = try? await product.subscription?.status.last
}
}