Swift StoreKit2 - How to know which subscription the user subscribed to?

531 Views Asked by At

I'm currently attempting to hook my app into storekit2 using the testing config file grabbing information from App Store Connect. Right now I can see the renewing subscription choices (all under one one group id so only one can be picked at a time) and can "subscribe" to one and down/upgrade to another.

In case you're stuck here I'm doing this through the view: SubscriptionStoreView(groupID: subscriptionGroupID, visibleRelationships: .all) but also implemented it, prior to that handy call, through another tutorial that created a class PurchaseManager that handled everything including physically generating the subscribe buttons. It can be found here: https://www.revenuecat.com/blog/engineering/ios-in-app-subscription-tutorial-with-storekit-2-and-swift/#h-step-7-sharing-active-purchases-with-extensions

My issue now is how to know which subscription, or maybe none, the user is subscribed to. Right now I want to display different information based on which subscription is subscribed to but I have no way to know what they have. Currently I attempted to solve this by grabbing the Transaction.currentEntitlements but it always prints only one product and it's always the same one regardless of which one is actually subscribed to.

Thanks for any help or direction you can provide.

Update: I am currently using a for statement to grab the Transaction.currentEntitlements and realized it always seemed to print the same as the renew period wasn't up. To clear the current purchases you can use the debug buttons: https://developer.apple.com/forums/thread/692574

Right now I find the subscription ID with:

func findSubscriptionPurchased() async -> String? {
    for await result in Transaction.currentEntitlements {
        guard case .verified(let transaction) = result else {
           continue
        }
        return transaction.productID
    }
    return nil // No purchased subscription
}

And plan to compare the result with my hardcoded product IDs. Is there a better way?

0

There are 0 best solutions below