SDK configuration

Initialising the SDK

The initialisation must be done as soon as possible to catch the purchase . On iOS, initialise the SDK in your AppDelegate method didFinishLaunchingWithOptions to allow promoted In-App Purchase and support PSD2. This initialisation will allow tyou to access producst prices instantly later in the app.

You will need an API Key that you can find in your App settings in the Purchasely Console.

import Purchasely

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    Purchasely.start(withAPIKey: "API_KEY", appUserId: "USER_ID")
	return true
}

The userID parameter is optional and allows you to associate the purchase to a user instead of a device.

The eventDelegate/eventListener parameter is optional and allows you to listen to all purchases events. You should implement it at least to know when the purchase is successfull.

The uiDelegate / uiListener parameter is optional and allows you to override UI dialog presented to user in case of error or success.

The logLevel parameter is optional and will display logs from the SDK according to the level set. We advise you to set it to warning or error for production

The stores parameter (for Android apps) is optional but purchase won't work without it. You need to pass a list of stores that are enabled for your application. The first store available in the user device will be the store used to make a purchase. In this sample, Google Play Billing will be used if available in user device, Huawei Mobile Services will be used otherwise.

The observerMode parameter is optional and allows you to use Purchasely with another In-App purchase system to prepare a migration. More details in our dedicated section.

Setting-up the User Id

Once your user is logged in and you can send us a userId, please do it otherwise the purchase will be tied to the device and your user won't be able to enjoy from another device. Setting it will allow you to tie a purchase to a user to use it on other devices.

This ID will be passed to the Webhook so that your backend can identify the user and unlock the access. If our backend made a migration of user purchases and notified your backend, we will set the refresh variable in the callback to true.

Purchasely.userLogin(with: "123456789")

To remove the user (logged out) you can perform a :

Purchasely.userLogout()

If your app allows anonymous purchases, keep the AppUserId to nil and have a look at our article.

Notifying the SDK when the app is ready / loaded

The SDK needs to display messages above your UI. It can be the continuation of a purchase started on the App Store, the result from a notification linking to our product, …

Your app needs to tell Purchasely SDK when it is ready to be covered by our UI.

This is done to handle cases like:

  • a loading screen that dismisses upon completion

  • an on boarding that needs to be displayed before purchasing

  • a subscribe process mandatory for app usage

When your app is ready, call the following method and the SDK will handle the continuation of whatever was in progress (purchase, push message, …)

This is mandatory to be able to handle Promoted In-App Purchases and Deeplinks automations.

// Call me in your viewDidAppear
Purchasely.isReadyToPurchase(true)

You can set it back to false when the app goes in the background when you have a screen that blocks UI in background mode and that is dismissed when the app is in foreground (like in banking apps).

Presenting products

Purchasely handles all the presentation of your products (aka paywalls) which are configured in the back office.

You can ask for the SDK to give you the UIViewController / androidx.fragment.app.Fragmentpresenting the purchase by calling the following :

let paywallCtrl = Purchasely.presentationController(with: "my_presentation_id"
                                                    completion: { (result, plan) in
    switch result {
        case .purchased:
            break
        case .restored:
            break
        case .cancelled:
            break
        @unknown default:
				    break
    }
})
present(paywallCtrl, animated: true)

The callback PLYProductViewControllerResult(iOS) / ProductViewResultListener (Android) is optional, you can set to null if you do not need it.

You can be alerted if the purchase was made by listening to the Notifications or by implementing the optional completion block

You can also get the presentation of a specific product of plan:

// Default presentation for a product
let paywallProductCtrl = Purchasely.productController(with: "my_product_id")
present(paywallProductCtrl, animated: true)

// Default presentation for a plan
let paywallPlanCtrl = Purchasely.planController(with: "my_plan_id")
present(paywallPlanCtrl, animated: true)

Unlock content / service once a purchase is made

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 catch it like this

 NotificationCenter.default.addObserver(self, selector: #selector(reloadContent(_:)), name: .ply_purchasedSubscription, object: nil)

And use it like that

 @objc func reloadContent(_ notification: Notification) {
     // Reload the content
 }

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

Close SDK (Android)

When done with Purchasely, you should call close() to remove all references to your activities. This method must only be called when you won't be using our SDK in the current user session. If you need to use our SDK again after calling close() then you need to call Purchasely.Builder() and Purchasely.start().

For example you can call this method in the onDestroy() method of your main activity

override fun onDestroy() {
    super.onDestroy()
    Purchasely.close()
}

Dernière mise à jour

© Purchasely 2020-2023