0

I followed this video: https://www.youtube.com/watch?v=0H2SdKf4ot0 which was immensely helpful in helping me get RC setup in my SwiftUI app. I was able to get purchasing working for a single subscription using a button:

VStack {
                    Button(action: {
                        Purchases.shared.offerings { offerings, error in
                            if let packages = offerings?.current?.availablePackages {
                                Purchases.shared.purchasePackage(packages.first!, { transaction, purchaserInfo, error, userCancelled in
                                    print("TRANSACTION: \(transaction)")
                                    print("PURCHASER INFO: \(purchaserInfo)")
                                    print("USER CANCELED: \(userCancelled)")

                                    if purchaserInfo?.entitlements["Pro"]?.isActive == true {
                                    }
                                })
                            }
                        }

                    }, label: {
                        Text("1 month")
                            .foregroundColor(.white)
                            .frame(maxWidth: 280)
                    })

                }

The challenge I’m facing now is that I have 3 purchase options for my one Entitlement : monthly sub, annual sub, and lifetime. These are all ready configured in App Store Connect and on RC.

Since this example uses packages.first (and correctly grabs the monthly sub) how do I configure my yearly and lifetime buttons? There’s no packages.second or packages.third

Appreciate any insight. Thanks!

phiredrop
  • 182
  • 1
  • 11

1 Answers1

0

Seems like the widely used answer to this is just using a ForEach loop. Thanks to Josh over at RC Forums! https://community.revenuecat.com/general-questions-7/multiple-subscription-buttons-swiftui-1290?postid=3962#post3962

struct MyView: View {

  @State private var packages = [Purchases.Package]()

  var body: some View {
    VStack {
      ForEach(packages, id: \.self) { package in
        Button {
          // Purchase package
        } label: {
          Text(package.localizedPriceString)
        }
      }
    }.onAppear {
      Purchases.shared.offerings { (offerings, error) in
        if let offering = offerings?.current {
          packages = offering.availablePackages
        }
      }
    }
  }
}
phiredrop
  • 182
  • 1
  • 11