📦 SwiftyLaunch Modules
💰 InAppPurchaseKit
Lock Actions behind a Paywall

Lock Actions behind a Paywall

InAppPurchaseKit (SwiftyLaunch Module) - Lock Actions behind a Paywall

Maybe you want to lock a specific feature behind a paywall in Swift. For this, SwiftyLaunch provides a couple of ways to do this.

By directly checking the subscription state

You can simply check if the user has premium access, by checking the subscriptionState variable of the InAppPurchases object.

import InAppPurchaseKit
import SharedKit
 
/// Will vibrate if the user is subscribed to the premium plan
func vibrateForPremiumUsers(iap: InAppPurchases) {
    if iap.subscriptionState == .subscribed {
        Haptics.notification(type: .success)
    }
}
InAppPurchases.swift
public class InAppPurchases: ObservableObject {
    @Published public private(set) var subscriptionState: SubscriptionState = .notSubscribed
}

Recommended: By calling the executeIfGotPremium() function

We recommend to use the executeIfGotPremium() function instead. Just pass whatever needs to be executed if the user got premium. If the user doesn't have premium, the function will present the user with the paywall.

import InAppPurchaseKit
import SharedKit
 
/// Will vibrate if the user is subscribed to the premium plan
func vibrateForPremiumUsers(iap: InAppPurchases) {
    iap.executeIfGotPremium {
        Haptics.notification(type: .success)
    }
}

The function signature is as follows:

InAppPurchases.swift
public class InAppPurchases: ObservableObject {
    public func executeIfGotPremium(
        otherwise consequence: NoPremiumConsequence = .showPaywall,
        _ closure: () -> Void
    ) { }
}
  • otherwise - (Optional) action to perform in case the user is not signed in. .showInAppNotification to show an in-app notification indication that the feature is behind the paywall or .showPaywall to directly show the paywall (Defaults to showPaywall).
  • closure - What to execute if the user has premium.

The function comes in two flavors: synchronous and asynchronous. The async version is available by just using await.