React to Changes to Entitlements

This guide explains how your app can react and modify its state based on entitlements being made active and inactive.

Once you have Nami set up and have integrated the SDK into your application, you'll also want to know how you can detect and react to entitlements being changed.

Entitlements are used to control access to paid features and content in your app. The Nami platform models your customers' current entitlement state from signals generated on-device and that we receive on our servers if you have set us up to receive store notifications.

It is important that your app react to changes to entitlement state to ensure that you are granting access correctly to users that have made a purchase and are revoking access when they are no longer paying you.

There are two main ways to do this with the Nami SDK:

  • Manually check to see if an entitlement has been granted.
  • Register a callback to take an action when an entitlement has been granted or revoked.

Manually check to see if a purchase has been made

The Nami SDKs provide a simple call to check if a particular entitlement is currently active. Be sure to use the correct entitlement reference ID you have set in the Control Center.

let active = NamiEntitlementManager.isEntitlementActive("premium_access")

Using a callback to react to changes in entitlements

If you do not wish to manually check the entitlement state at specific spots in your code, you might instead want to have your app react to changes to the list of active entitlements that your user currently has access to.

Nami offers a callback to use in this case that will be activated whenever entitlements change. This could mean either an entitlement being granted or revoked.

In Swift and Objective-C this is done by registering a handler. In React Native, simply add a listener for the correct event, which will contain an array of active Entitlement objects.

NamiEntitlementManager.registerChangeHandler { (activeEntitlements: [NamiEntitlement]) in
  // loop over active entitlements
  for entitlement in activeEntitlements {
    let referenceId = entitlement.referenceId
    // Enable features based on referenceId