Question

How to detect status of product when purchasing?

  • 18 October 2022
  • 0 replies
  • 244 views

Following the docs and starter project for iOS-SwiftUI its fairly straightforward in implementing purchases in apps.

However in StoreKit v1 you are able to switch on the SKPaymentTransaction to get the transactionState:

  • case .purchasing:
  • case .purchased:
  • case .restored:
  • case .failed:
  • case .deferred:

This is great as for each product we can get the status when a user is attempting to buy a product.

When I was using StoreKit v1, I was able to add an animation to the product when it was in the purchasing state (like the App Store spinner), and then once it had been purchased I was able to disable the button and mark it as “Purchased” instead of the price.

I’ve been trying to replicate this with RevenueCat API but cant seem to be able to figure out how to do it - so I’m looking to the community for some help 😅

 

Code that I have:

// -- MainView
...
if let availablePackages = subscriptionManager.offerings?.current?.availablePackages {
ForEach(availablePackages) { package in
PackageCell(package: package) { (package) in
isProcessing = true
do {
let result = try await Purchases.shared.purchase(package: package)
self.isProcessing = false
} catch {}
}
}
}


// -- PackageCell
struct PackageCell: View {
let package: Package
let onSelection: (Package) async -> Void
var body: some View {
Button {
Task {
await onSelection(package)

// -- remove user defaults
for identifier in SubscriptionManager.shared.productIdentifiers {
UserDefaults.shared.removeObject(forKey: identifier)
}
// -- set for this product
UserDefaults.shared.set(true, forKey: package.storeProduct.productIdentifier)
}
} label: {
HStack {
Text(package.storeProduct.localizedTitle)
Text(package.storeProduct.localizedPriceString)
}
}
}
}

What I currently have is remove all product identifiers from the UserDefaults, then add the currently active subscription. I will then use that as whether the cell should be .disabled(UserDefaults.shared.bool(forKey: package.storeProduct.productIdentifier))

 

However, since this is inside the cell block it is disabling it even if a user exits the purchase. But we can detect it in the ForEach(availablePackages) loop, with if result.userCancelled

But when I run any of the checks there it affects all the packages not only the selected one.

Is there a RevenueCat way of achieving this? 

 

Ideally, I’d like the states of unpurchased, purchasing, and purchased for the individual products, and be able to affect the current active item - regardless if it is subscription or consumable.


0 replies

Be the first to reply!

Reply