Setup Payments

Request payments for subscriptions or in-app purchases. Restore previous purchases.

Usage

Step 1. Add RevenueCat SDK to your project

  • Go to File -> Add Package Dependency.
  • In the search bar, enter this URL.
  • Choose the version you'd like to install.
  • Click Add Package
  • Keep selection for the RevenueCat package only

At this point, follow the RevenueCat documentation to setup your project, and come back once done.

Step 2. Start the Payment Service In your AppDelegate::didFinishLaunchingWithOptions just adds the following line:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    
    Payment.shared.start()
    return true
}

Step 3. Check if user is subscribed Often in times, you will need to check if the user is currently subscribed, when giving access to premium features for instance. Below is the code snippet you can use to quickly check if the user has an active subscription.

Payment.shared.isUserSubscribed { subscribed in
    if subscribed {
        // Handle subscribed user, i.e showing to the user they are already subscribed, or enabling some premium features
    } else {
        // Handle user not subscribed, i.e showing a paywall
    }
}


Step 4. Restore user subscription One of the option that your app will have to provide, to make sure the Apple review process goes smoothly, is the ability to restore purchases. Whether it's a subscription or a in-app purchase (i.e. Lifetime offer) below is the code snippet that should be triggered upon user request of restoring a plan.

Payment.shared.restorePurchases {
    // Handle success
} failure: { error in
    // Handle failure, like a message informing the user that there was no subscription to restore
}


Step 5. Start the purcharse process for a product And for the gand finale. When a user decides to purchase a plan your app offer, whether is a subscription or an in-app purchase, you can use the code below to trigger the purchase flow.

Payment.shared.purchaseProduct(productId: product) {
    // Handle success, like a message informing the user they are now subscribed or any UI changes
} failure: { error in
    // Handle failure, like a message informing the user that there was a problem purchasing the plan
    // This error can only be due to Apple IAP service having issues
}