Solved

How to get the Entitlement name from a Package?

  • 13 July 2023
  • 4 replies
  • 209 views

Badge +3

Right now I have 1 entitlement-id named powersEntitlementIDand I’m using it in iOS which works fine:

Purchases.shared.getOfferings { [weak self](offerings, error) in

    guard let package = offerings?.all.first?.value.availablePackages.first else { return }

    self?.showPurchaseActionSheet(for: package)

}

 

func showPurchaseActionSheet(for package: Package) {

    Purchases.shared.purchase(package: package) { (transaction, customerInfo, error, userCancelled) in }

}

 

Using that in my PurchasePowersController, it shows me the correct action sheet. But I intend on adding a new productID/EntitlementID and I will use them in different ViewControllers. How can I get the EntitlementID/name from a Package object?

For example if I had 2 entitlements named powersEntitlementID and energyEntitlementID. I can use the following code in PurchasePowersController and PurchaseEnergyController 

 

class PurchasePowersController: UIViewController {

 

    override func viewWDidAppear(_ animated: Bool) {

        super.viewDidAppear(animated)

 

        presentPurchase(for: “powersEntitlementID”)

    }

}

 

class PurchaseEnergyController: UIViewController {

 

    override func viewDidAppear(_ animated: Bool) {

        super.viewDidAppear(animated)

 

        presentPurchase(for: “energyEntitlementID”)

    }

}

 

// this is a global function

func presentPurchase(for entitlementID: String) {

 

    Purchases.shared.getOfferings { [weak self](offerings, error) in

 

        guard let offerings = offerings else { return }

         

        for dict in offerings.all {

            

            let offering = dict.value

            let packages = offering.availablePackages

 

            // I need to compare entitlementID to some property value on $0.??? ($0 is each package object in the packages array)

            if let indexOfItem = packages.firstIndex(where: { $0.??? == entitlementID }) {

 

                let package = packages[indexOfItem]

                self?.showPurchaseActionSheet(for: package)

                break

            }

        }

    }

}

 

 

icon

Best answer by lance-samaria 18 July 2023, 07:54

View original

4 replies

Userlevel 4
Badge +6

Hey @lance-samaria,

At the time, it’s not possible to get the entitlement id from the package object. You can check out our SDK reference to see what properties are attached to each object: https://revenuecat.github.io/purchases-ios-docs/4.25.0/documentation/revenuecat

Badge +3

Hi @kaitlin , thanks for the info.

 

How do I get a package for a specific entitlement? Using my above examples, I want to get the package for powersEntitlementID and energyEntitlementID, in separate ViewControllers. Purchases.shared.getOfferings { }, doesn't offer a way to do that.

 

 

Badge +3

@kaitlin I found the answer. Hopefully this helps other people.

 

When you want to get a specific offering, you need to use the productID that you are looking for. For example if I had 2 productIDs in the AppStore named powers_599_1w_1w0 and energy_999_1m, I would use those for comparison against package.storeProduct.productIdentifier.

 

In short, for revenueCat’s iOS Package type, has a storeProduct property, and storeProduct has a productIdentifier property that equates to whichever productID you’re looking for. The below code is tested and 100% works.

 

func presentPurchase(for productID: String) {

 

    Purchases.shared.getOfferings { [weak self](offerings, error) in

 

        guard let offerings = offerings else { return }

         

        for dict in offerings.all {

             

            let offering = dict.value

            let packages = offering.availablePackages

            if let indexOfItem = packages.firstIndex(where: { $0.storeProduct.productIdentifier == productID }) {

 

                let package = packages[indexOfItem]

                self?.showPurchaseActionSheet(for: package)

                break

            }

        }

    }

}

 

For Android, you want to check either of these, it seems they both produce the same GooglePlayStore productID values

val playStore_productID_1 = package.product.googleProduct?.productId
val playStore_productID_2 = package.product.purchasingData.productId

However, for Android, I also used this below, but I got the RevenueCat productID, which for some weird reason is different from my PlayStoreID

val revenueCat_productID = package.product.id

 

For example

 

fun presentPurchaseFor(productID: String, activity: AppCompatActivity) {
    Purchases.sharedInstance.getOfferingsWith({ error ->        // … error    }) { offerings →
         offerings.current?.availablePackages?.takeUnless {             it.isEmpty()         }?.let { packages →             if (packages.isNotEmpty()){
                 packages.forEach { pkg: Package →

 

                                              if (pkg.product.googleProduct?.productId == productID) {

                                                  // this is the package you’re looking for

                                                 Purchases.sharedInstance.purchaseWith( PurchaseParams.Builder(activity, pkg).build() {...}

                                              }

 

                                              // *** OR ***

 

                                              if (pkg.product.purchasingData.productId == productID) {

                                                  // this is the package you’re looking for

                                                 Purchases.sharedInstance.purchaseWith( PurchaseParams.Builder(activity, pkg).build() {...}

                                              }

 

                                    }

                             }

          }

}

 

 

 

}

Badge

@kaitlin is there any way to request this feature of getting the entitlement, that a specific package is included to? We are in a situation where we will have to display products in two different sections of the app, based on the entitlement. We don’t want to hard-code and switch on the product-identifier, if we e.g. want to add a new product without updating the app. :)

Reply