iOS SDK

Guide for integrating Purchasely SDK inside your iOS app

Installation

CocoaPods

You can use CocoaPods to install Purchasely by adding it to your Podfile:

pod 'Purchasely'

Carthage

You can use Carthage to install Purchasely by adding it to your Cartfile:

binary "https://raw.githubusercontent.com/Purchasely/Purchasely-iOS/master/Purchasely.json"

Swift Package Manager

You can integrate Purchasely as a Swift package by adding the following URL to the public package repository that you can add in Xcode:

https://github.com/Purchasely/Purchasely-iOS

Initialising the SDK

In the AppDelegate method didFinishLaunchingWithOptions initialize the SDK. This must be done at this very moment to catch the purchase requests made from the App Store directly (promoting in app purchases), previous incomplete transactions, renewal, …

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

Swift
Objective-C
Swift
import Purchasely
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
Purchasely.start(withAPIKey: "API_KEY", appUserId: "USER_ID")
return true
}
Objective-C
#import <Purchasely/Purchasely-Swift.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[Purchasely startWithAPIKey:@"API_KEY" appUserId:@"USER_ID" eventDelegate:nil uiDelegate:nil logLevel: LogLevelInfo];
return YES;
}

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

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.

Swift
Objective-C
Swift
Purchasely.setAppUserId("123456789")
Objective-C
[Purchasely setAppUserId:@"123456789"];

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

Swift
Objective-C
Swift
Purchasely.setAppUserId(nil)
Objective-C
[Purchasely setAppUserId: nil];

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.

Swift
Objective-C
Swift
Purchasely.isReadyToPurchase(true)
Objective-C
[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 presenting the purchase by calling the following :

Swift
Objective-C
Swift
let paywallCtrl = Purchasely.productController(for: "my_product_id")
present(paywallCtrl, animated: true)
Objective-C
UIViewController *ctrl = [Purchasely productControllerFor:@"my_product_id" with:nil];
[self presentViewController:ctrl animated:YES completion:nil];

Since Purchasely 1.1.0 you can choose between multiple presentations by giving a presentationId :

Swift
Objective-C
Swift
let paywallCtrl = Purchasely.productController(for: "my_product_id", with: "my_presentation_id")
present(paywallCtrl, animated: true)
Objective-C
UIViewController *ctrl = [Purchasely productControllerFor:@"my_product_id" with:"my_presentation_id"];
[self presentViewController:ctrl animated:YES completion:nil];

You can be alerted if the purchase was made by listening to the Notifications