1

I have the following swift code that allows me to purchase a subscription inside my IOS app. I'm using the revenue cat SDK.

 Purchases.shared.logIn(uid) { (purchaserInfo, created, error) in
            Purchases.shared.offerings { (offerings, error) in
                if let package = offerings?.current?.lifetime {
                    Purchases.shared.purchasePackage(package) { (transaction, purchaserInfo, error, userCancelled) in
     

The issue is that this code takes a while to run. The user waits for the purchase to go through. I think it's getting the list of products before making a call to Apple. Is there a way to optimize this code by loading the packages on app load in the AppDelegate file for example?

Chris Hansen
  • 7,813
  • 15
  • 81
  • 165

1 Answers1

1

Yes, as you described you can call the getOfferings method onAppear or in the AppDelegate and save the packages in an object.

Init the object

@State private var packages: [Package]? = nil

Call the Purchases.shared.getOfferings method onAppear or in AppDelegate and save the catches packages in the object.

Purchases.shared.getOfferings { (offerings, error) in
    if let offerings = offerings {
        if let current = offerings.current {
            packages = current.availablePackages
        } else if let sandbox = offerings.offering(identifier: "sandbox") {
            packages = sandbox.availablePackages
        }
    } else {
        if let error = error {
            print("ERROR: \(error)")
        }
    }
}

The "Buy" Button calls then the Purchases.shared.purchase with the selected package.

Purchases.shared.purchase(package: package) { (transaction, customerInfo, error, userCancelled) in
    if let customerInfo = customerInfo,
       let product = customerInfo.entitlements.all[package.storeProduct.productIdentifier],
       product.isActive == true {
        // Activate the In-App Purchases
    }
    if let err = error {
        // Handle Error
    }
}

Note: I also experienced with this technique a delay when tapping the Buy button in development and testing environment, but didn't had any delays in production environment.

Jonas Deichelmann
  • 3,513
  • 1
  • 30
  • 45