Skip to main content
Solved

How to implement purchase(package:) using SubscriptionStoreView?

  • December 11, 2024
  • 1 reply
  • 178 views

Forum|alt.badge.img+3

Hi, I’m developing iOS 18+ app with RevenueCat 5.12.1.

I found SubscriptionStoreView is almost perfect for me to make a paywall.

My earlier code without SubscriptionStoreView used a custom purchase button like this.

1 var buttonToPurchase: some View {
2
3 let packages = offering?.availablePackages ?? []
4 let package = packages.first ?? nil
5
6 Button {
7
8 guard let currentPackage = package else { return }
9
10 Purchases.shared.purchase(package: currentPackage) { (transaction, info, error, userCancelled) in
11
12 if info?.entitlements[Constants.entitlementID]?.isActive == true {
13 navigationViewModel.path.removeAll()
14 } else if let error = error {
15 self.error = error as NSError
16 self.displayError = true
17 }
18 }
19
20 } label: {
21 Text("Continue")
22 }
23 }

 

I don’t know how to apply Purchase.shared.purchase(package:) into SubscriptionStoreView.

I know SubscriptionStoreView provides two modifiers

.onInAppPurchaseStart { product in … }

.onInAppPurchaseCompletion { product, result in … }

I also know SubscriptionStoreView displays inherently ‘subscribe button’.

But my code below seems to be wrong because it triggers subscription sheet twice, and I wonder, when a firstly triggered subscription is completed, if RevenueCat server is appropriately tracking the subscription with correct anonymousID for customer.

1SubscriptionStoreView.forOffering(offering!) {
2 premiumDescription
3}
4.onInAppPurchaseCompletion { product, result in
5 let packages = offering?.availablePackages ?? []
6 let package = packages.first ?? nil
7
8 if case .success(.success(let transaction)) = result {
9 print("Purchased successfully: \(transaction.signedDate)")
10
11 guard let currentPackage = package else { return }
12
13 Purchases.shared.purchase(package: currentPackage) { (transaction, info, error, userCancelled) in
14
15 if info?.entitlements[Constants.entitlementID]?.isActive == true {
16 navigationViewModel.path.removeAll()
17
18 } else if let error = error {
19 self.error = error as NSError
20 self.displayError = true
21 }
22 }
23 } else {
24 print("Purchase was cancelled.")
25 }
26}

Please help me 🙏 Thanks in advance!! 

Best answer by bg_kim

I figured out that RevenueCat observer mode can support the purchase logic from the default purchase button in SubscriptionStoreView.
 

https://www.revenuecat.com/docs/migrating-to-revenuecat/sdk-or-not/finishing-transactions
 

View original
Did this post help you find an answer to your question?
This post has been closed for comments

1 reply

Forum|alt.badge.img+3
  • Author
  • Helper
  • 3 replies
  • Answer
  • December 16, 2024

I figured out that RevenueCat observer mode can support the purchase logic from the default purchase button in SubscriptionStoreView.
 

https://www.revenuecat.com/docs/migrating-to-revenuecat/sdk-or-not/finishing-transactions
 


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings