When you display a paywall with Purchasely.presentation you have a closure for the result of user action PLYProductViewControllerResult with three possible values
Purchased
Restored
Cancelled\
You also have as a second argument the plan bought or restored by the user, it is set to nil if no purchase was made.
This is the preferred way to get notified when a purchase or restoration was made from a Purchasely paywall.
When a purchase or restoration is made, you can listen to our notification.
This is the method to use in parts of your application where you wish to unlock some features after a purchase was made but you should only use it to unlock content, not to notify your server of a purchase or check the current state of user subscription.
You will be notified about the purchase but not about the plan purchased. If you want to know the plan, you must use the method describe above with paywall displayed
Once the purchase is made to Apple Servers, registered in our systems, Purchasely sends a local Notification in the NotificationCenter. You can use it to unlock the content or refresh it.
// You can use LiveDataPurchasely.livePurchase().observe(this, (plan) -> {reloadContent(plan);});// Or PurchaseListenerPurchasely.setPurchaseListener(purchaseListener);
Purchasely.addPurchasedListener(() => {// User has successfully purchased a product, reload content});
Purchasely.purchasedSubscription(() => {// User has successfully purchased a product, reload content});
Purchasely.listenToPurchases(() {print('User has purchased a product');});//when no longer needed, remove listenerPurchasely.stopListeningToPurchases();
And use it like that
@objcfuncreloadContent(_notification: Notification) {// Reload the content }
- (void)reloadContent: (NSNotification *)aNotification {
// Reload the content
}
privatefunreloadContent(plan: PLYPlan?) {//Reload the content}//Instance of PurchaseListenerprivateval purchaseListener: PurchaseListener=object : PurchaseListener {overridefunonPurchaseStateChanged(@NotNull state: State) {if (state is State.PurchaseComplete) {reloadContent(state.plan) } elseif (state is State.RestorationComplete) {reloadContent(state.plan) } }}
privatevoidreloadContent(@NullablePLYPlan plan) {//Reload the content}//Instance of PurchaseListenerprivatePurchaseListener purchaseListener =newPurchaseListener() { @OverridepublicvoidonPurchaseStateChanged(@NotNullState state) {if(state instanceofState.PurchaseComplete) {PLYPlan plan = ((State.PurchaseComplete) state).getPlan();reloadContent(plan); } elseif(state instanceofState.RestorationComplete) {PLYPlan plan = ((State.RestorationComplete) state).getPlan();reloadContent(plan); } }};
For example, this can be done in every controller that displays premium content. That way you won't have to reload the content each time the controller is displayed unless a payment was made