Right now I have 1 entitlement-id named powersEntitlementID, and 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
}
}
}
}