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")
BOOL active = [NamiEntitlementManager isEntitlementActive: @"premium_access"];
val active = NamiEntitlementManager.isEntitlementActive("premium_access")
boolean active = NamiEntitlementManager.isEntitlementActive("premium_access")
NativeModules.NamiEntitlementManagerBridge.isEntitlementActive("premium_access", (active) => { 
    if (active) {
    // react to entitlement being active
  }
});
var 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.registerEntitlementsChangedHandler { (activeEntitlements: [NamiEntitlement]) in
  // loop over active entitlements
  for entitlement in activeEntitlements {
    let referenceID = entitlement.referenceID
    // Enable features based on referenceID
  }
}
[NamiEntitlementManager registerEntitlementsChangedHandler:^(NSArray<NamiEntitlement *> * _Nonnull activeEntitlements) {
  for (NamiEntitlement *entitlement in activeEntitlements) {
    NSString *referenceID = entitlement.referenceID;
    // take any appropriate action based on the active entitlement
  }
}];
NamiEntitlementManager.registerEntitlementChangeListener { activeEntitlements ->
    if (activeEntitlements.isEmpty()) {
        // No active entitlements anymore
    } else {
        activeEntitlements.forEach { namiEntitlement ->
            val referenceId = namiEntitlement.referenceId
            // Enable features based on referenceId
         }
    }
}
NamiEntitlementManager.registerEntitlementChangeListener((Function1<List<NamiEntitlement>, Unit>) namiEntitlements -> {
    if(namiEntitlements.isEmpty()) {
        // No active entitlements anymore
    } else {
        for(NamiEntitlement namiEntitlement: namiEntitlements) {
            String referenceId = namiEntitlement.getReferenceId();
            // Enable features based on referenceId
        }
    }
    return Unit.INSTANCE;
});
const { NamiEmitter } = NativeModules;
const eventEmitter = new NativeEventEmitter(NamiEmitter);

const onEntitlementsChanged = (event) => {
  // Add code to check for entitlements activating or deactivating features
  console.log("ExampleApp: Data for entitlements changed ", event.activeEntitlements);
}

useEffect(() => {
  eventEmitter.addListener('EntitlementsChanged', onEntitlementsChanged);
}
NamiEntitlementManager.entitlementChangeEvents().listen((activeEntitlements) {
    if (activeEntitlements.isNotEmpty) {
        // "Active entitlements found!"
        activeEntitlements.forEach((element) {
            var referenceId = element.referenceId;
            // Enable features based on referenceId
        });
    } else {
        // "No active entitlements anymore"
    }
});

Did this page help you?