I'm trying to implement StoreKit in my project, specifically, the auto-renewable subscriptions, using Apple's implementing StoreSit2 example.
I refactored Apple's project to only include a yearly subscription based on what I need for my app. I uploaded the refactored project on Github. I did everything exactly as the project is setup, however, I get this error in the files: Store and StatusInfoView at the following line: In "Store" line 214:
subscriptionGroupStatus = try? await subscriptions.first?.subscription?.status.first?.state
Error:
Cannot assign value of type 'Product.SubscriptionInfo.RenewalState??' to type 'RenewalState?' (aka 'Optional<Product.SubscriptionInfo.RenewalState>')
In "StatusInfoView" line 134:
subscriptionStatus = try? await product.subscription?.status.last
Error:
Cannot assign value of type 'Product.SubscriptionInfo.Status??' to type 'Product.SubscriptionInfo.Status?'
  The project on Github Does not produce error It shows after I copy paste the files to my Xcode project.
I can't figure out how to fix value assignment.
Any help would be greatly appreciated
@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
}
}
I figured it out. The project from Apple was written in Swift 5. When I refactored the code and moved it to my project, which was written in Swift 4, it threw that syntax error at.
I updated my project to Swift 5 and the error was gone.