RevenueCat

RevenueCat Documentation

Here you will find guides for getting RevenueCat setup and references for our SDKs and APIs.

Guides    Discussions

iOS

Instructions for integrating Purchases for iOS

Installation

Purchases for iOS can be installed either via CocoaPods or Carthage.

Install via CocoaPods

Add the following to your Podfile

pod 'Purchases'

And then run:

pod install

This will add Purchases.framework to your workspace.

Install via Carthage

Add the following to your Cartfile

github "revenuecat/purchases-ios"

The run carthage update

carthage update --no-use-binaries

Make sure to include the --no-use-binaries to work around a bug in the most recent Carthage release.

Setting up RCPurchases

RCPurchases should be created as soon as you have a user ID. If you do not have your own user IDs provided by an account system, omit the appUserID parameter from the constructor and we'll create and cache one on the device for you. See User IDs for more info.

You can get your API key from app settings in the dashboard.

import UIKit

import StoreKit
import Purchases


@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var purchases: RCPurchases?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        self.purchases = RCPurchases(apiKey: "my_app_api_key")
        self.purchases!.delegate = self

        return true
    }
}

extension AppDelegate: RCPurchasesDelegate {
    func purchases(_ purchases: RCPurchases, completedTransaction transaction: SKPaymentTransaction, withUpdatedInfo purchaserInfo: RCPurchaserInfo) {
        handlePurchaserInfo(purchaserInfo)
    }

    func purchases(_ purchases: RCPurchases, receivedUpdatedPurchaserInfo purchaserInfo: RCPurchaserInfo) {
        handlePurchaserInfo(purchaserInfo)
    }

    func purchases(_ purchases: RCPurchases, failedToUpdatePurchaserInfoWithError error: Error) {
        showError(error)
    }

    func purchases(_ purchases: RCPurchases, failedTransaction transaction: SKPaymentTransaction, withReason failureReason: Error) {
        showError(failureReason)
    }
    
    func purchases(_ purchases: RCPurchases, restoredTransactionsWith purchaserInfo: RCPurchaserInfo) {
        handlePurchaserInfo(purchaserInfo)
    }
    
    func purchases(_ purchases: RCPurchases, failedToRestoreTransactionsWithError failureReason: Error) {
    		showError(failureReason)
    }
}

@import Purchases;

@interface AppDelegate () <RCPurchasesDelegate>
  
@property (nonatomic, readwrite) RCPurchases *purchases;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application 
  didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.purchases = [[RCPurchases alloc] initWithAPIKey:@"my_app_api_key"];
    self.purchases.delegate = self;
    
    return YES;
}
- (void)purchases:(RCPurchases *)purchases
completedTransaction:(SKPaymentTransaction *)transaction
  withUpdatedInfo:(RCPurchaserInfo *)purchaserInfo
{
    [self handleNewPurchaserInfo:purchaserInfo];
}

- (void)purchases:(RCPurchases *)purchases
failedTransaction:(SKPaymentTransaction *)transaction
       withReason:(NSError *)failureReason
{
    [self showError];
}

- (void)purchases:(RCPurchases *)purchases 
  receivedUpdatedPurchaserInfo:(RCPurchaserInfo *)purchaserInfo
{
    // This method will be called on launch and when returning after a while
    [self handleNewPurchaserInfo:purchaserInfo];
}

- (void)purchases:(nonnull RCPurchases *)purchases failedToUpdatePurchaserInfoWithError:(nonnull NSError *)error {
    // Optionally handle error, usually when there is no connection
}

- (void)purchases:(RCPurchases *)purchases restoredTransactionsWithPurchaserInfo:(RCPurchaserInfo *)purchaserInfo {
    [self handleNewPurchaserInfo:purchaserInfo];
}

- (void)purchases:(nonnull RCPurchases *)purchases failedToRestoreTransactionsWithError:(nonnull NSError *)error {

}

@end

Displaying Available Products

Use entitlements to power your purchasing UI.

func displayUpsellScreen() {
	self.purchases?.entitlements({ (ents) in
  	let vc = UpsellController()
    vc.entitlements = ents
    presentViewController(vc, animated: true, completion: nil)
  })
}
[self.purchases entitlements:^(NSDictionary<NSString *,
                                            RCEntitlement *> *entitlements) {
	UpsellViewController *vc = [[UpsellViewController alloc] init];
  vc.entitlements = entitlements;
  [self presentViewController:vc animated:YES completion:nil];
}];