Subscription status

With Adapty you don't have to hardcode product ids to check subscription status. You just have to verify that the user has an active access level. To do this, you have to call .getProfile() method:

Adapty.getProfile { result in
    if let profile = try? result.get() {
        // check the access
    }
}
Adapty.getProfile { result ->
    when (result) {
        is AdaptyResult.Success -> {
            val profile = result.value
            // check the access
        }
        is AdaptyResult.Error -> {
            val error = result.error
            // handle the error
        }
    }
}
Adapty.getProfile(result -> {
    if (result instanceof AdaptyResult.Success) {
        AdaptyProfile profile = ((AdaptyResult.Success<AdaptyProfile>) result).getValue();
        // check the access
      
    } else if (result instanceof AdaptyResult.Error) {
        AdaptyError error = ((AdaptyResult.Error) result).getError();
        // handle the error
    }
});
try {
  final profile = await Adapty().getProfile();
  // check the access
} on AdaptyError catch (adaptyError) {
  // handle the error
} catch (e) {
}
Adapty.GetProfile((profile, error) => {
  if (error != null) {
    // handle the error
    return;
  }
  
  // check the access
});
try {
	const profile = await adapty.getProfile();
} catch (error) {
  // handle the error
}

Response parameters:

  • Profile: an AdaptyProfile object. Generally, you have to check only access level status to determine whether the user has premium access to the app.

📘

The .getProfile method provides the most up-to-date result as it always tries to query the API. If for some reason (e.g. no internet connection), the Adapty SDK fails to retrieve information from the server, the data from cache will be returned. It is also important to note that the Adapty SDK updates AdaptyProfile cache on a regular basis, in order to keep this information as up-to-date as possible.

Below is a complete example of checking the user's access level.

Adapty.getProfile { result in
    if let profile = try? result.get(), 
           profile.accessLevels["premium"]?.isActive ?? false {
        // grant access to premium features
    }
}
Adapty.getProfile { result ->
    when (result) {
        is AdaptyResult.Success -> {
            val profile = result.value
            
            if (profile.accessLevels["premium"]?.isActive == true) {
                // grant access to premium features
            }
        }
        is AdaptyResult.Error -> {
            val error = result.error
            // handle the error
        }
    }
}
Adapty.getProfile(result -> {
    if (result instanceof AdaptyResult.Success) {
        AdaptyProfile profile = ((AdaptyResult.Success<AdaptyProfile>) result).getValue();
        
      	AdaptyProfile.AccessLevel premium = profile.getAccessLevels().get("premium");
        
      	if (premium != null && premium.isActive()) {
            // grant access to premium features
        }
    } else if (result instanceof AdaptyResult.Error) {
        AdaptyError error = ((AdaptyResult.Error) result).getError();
        // handle the error
    }
});
try {
  final profile = await Adapty().getProfile();
  if (profile?.accessLevels['premium']?.isActive ?? false) {
		// grant access to premium features
	}
} on AdaptyError catch (adaptyError) {
  // handle the error
} catch (e) {
}
Adapty.GetProfile((profile, error) => {
  if (error != null) {
    // handle the error
    return;
  }

  // "premium" is an identifier of default access level
  var accessLevel = profile.AccessLevels["premium"];
  if (accessLevel != null && accessLevel.IsActive) {
    // grant access to premium features
  }
});
try {
	const profile = await adapty.getProfile();
	
  const isActive = profile.accessLevels["premium"]?.isActive;
	if (isActive) {
		// grant access to premium features
	}
} catch (error) {
	// handle the error
}

📘

You can have multiple access levels per app. For example, if you have a newspaper app and sell subscriptions to different topics independently, you can create access levels "sports" and "science". But most of the time, you will only need one access level, in that case, you can just use the default "premium" access level.

Read more about access levels in the Access Level section.

Listening for subscription status updates

Whenever the user's subscription changes, Adapty will fire an event. In order to receive messages from Adapty, you need to make some additional configuration (depends on your platform):

Adapty.delegate = self

// To receive subscription updates, extend `AdaptyDelegate` with this method:
func didLoadLatestProfile(_ profile: AdaptyProfile) {
    // handle any changes to subscription state
}
Adapty.setOnProfileUpdatedListener { profile ->
    // handle any changes to subscription state
}
Adapty.setOnProfileUpdatedListener(profile -> {
    // handle any changes to subscription state
});
Adapty().didUpdateProfileStream.listen((profile) {
  // handle any changes to subscription state
});
// Extend `AdaptyEventListener ` with `OnLoadLatestProfile ` method:
public class AdaptyListener : MonoBehaviour, AdaptyEventListener {
  public void OnLoadLatestProfile(Adapty.Profile profile) {
    // handle any changes to subscription state
  }
}
// Create an "onLatestProfileLoad" event listener
adapty.addEventListener('onLatestProfileLoad', profile => {
	// handle any changes to subscription state
});

Whenever the user's subscription changes, Adapty will fire an event. There also will be an event at the start of the application, and the cached profile will be passed.

🚧

Make sure to set up App Store Server Notifications (for iOS) and Real-time Developer Notifications (RTDN) (for Android) to receive subscription updates without significant delays.