Present flows in Observer mode in iOS SDK

If you use Observer mode, you are responsible for your own purchase logic. But you can delegate the paywall UI to the Flow & Paywall Builder.

Design the screen in the builder, and the Adapty SDK renders it natively. Update its copy, layout, and pricing whenever you want, without an app release. When a user taps a purchase button, the SDK calls your code to complete the purchase.

This guide shows how to present that screen and handle the purchase call. If Adapty completes purchases for you, see Display flows & paywalls instead.

Before you start

You need:

Implementation

  1. Implement the AdaptyObserverModeResolver object:

    func observerMode(didInitiatePurchase product: AdaptyPaywallProduct,
                      onStartPurchase: @MainActor @Sendable @escaping () -> Void,
                      onFinishPurchase: @MainActor @Sendable @escaping () -> Void) {
           // use the product object to handle the purchase
           // call onStartPurchase / onFinishPurchase to notify AdaptyUI about the purchase progress
    }
    
    func observerModeDidInitiateRestorePurchases(onStartRestore: @MainActor @Sendable @escaping () -> Void,
                                                 onFinishRestore: @MainActor @Sendable @escaping () -> Void) {
           // call onStartRestore / onFinishRestore to notify AdaptyUI about the restore progress
    }
  2. Create a flow configuration object, passing your resolver as the observerModeResolver: parameter:

    do {
        let flowConfiguration = try await AdaptyUI.getFlowConfiguration(
            forFlow: flow,
            preferredLocales: Locale.preferredLanguages,
            observerModeResolver: <AdaptyObserverModeResolver>
        )
    } catch {
        // handle the error
    }

    Request parameters:

    ParameterPresenceDescription
    forFlowrequiredAn AdaptyFlow object obtained via Adapty.getFlow(placementId:). See Get flows and paywalls.
    observerModeResolverrequiredThe AdaptyObserverModeResolver you implemented above.
  3. Initialize the flow controller using AdaptyUI.flowController(with:delegate:):

    import AdaptyUI
    
    let visualFlow = try AdaptyUI.flowController(
        with: flowConfiguration,
        delegate: <AdaptyFlowControllerDelegate>
    )

    Request parameters:

    ParameterPresenceDescription
    flowConfigurationrequiredAn AdaptyUI.FlowConfiguration object containing visual details of the flow. See Get flows and paywalls.
    delegaterequiredAn AdaptyFlowControllerDelegate to listen to flow events. See Handle flow & paywall events.

    Returns:

    ObjectDescription
    AdaptyFlowControllerAn object representing the requested flow screen.
  4. Present the controller:

    present(visualFlow, animated: true)
Warning

Don’t forget to associate paywalls to purchase transactions. Otherwise, Adapty will not determine the source paywall of the purchase. Pass the variationId of the AdaptyPaywallProduct the user bought, not AdaptyFlow.variationId: a flow’s own variation ID doesn’t link the purchase to the flow.

After onFinishPurchase(), the flow runs the When pending outcome actions whatever happened, a canceled purchase included. Show any screen that depends on the purchase result, such as a thank-you screen, from your code: only your code knows the result. After onFinishRestore(), the flow runs the When restore completed actions, whatever happened.

Before you start presenting paywalls (Click to Expand)
  1. Set up initial integration of Adapty with the Google Play and with the App Store.
  2. Install and configure Adapty SDK. Make sure to set the observerMode parameter to true. Refer to our framework-specific instructions for iOS.
  3. Create products in the Adapty Dashboard.
  4. Configure paywalls, assign products to them, and customize them using Paywall Builder in the Adapty Dashboard.
  5. Create placements and assign your paywalls to them in the Adapty Dashboard.
  6. Fetch Paywall Builder paywalls and their configuration in your mobile app code.

  1. Implement the AdaptyObserverModeResolver object:

    func observerMode(didInitiatePurchase product: AdaptyPaywallProduct,
                      onStartPurchase: @MainActor @Sendable @escaping () -> Void,
                      onFinishPurchase: @MainActor @Sendable @escaping () -> Void) {
           // use the product object to handle the purchase
           // use the onStartPurchase and onFinishPurchase callbacks to notify AdaptyUI about the process of the purchase
    }
    
    func observerModeDidInitiateRestorePurchases(onStartRestore: @MainActor @Sendable @escaping () -> Void,
                                                 onFinishRestore: @MainActor @Sendable @escaping () -> Void) {
           // use the onStartRestore and onFinishRestore callbacks to notify AdaptyUI about the process of the restore
    }

    The observerMode(didInitiatePurchase:onStartPurchase:onFinishPurchase:) event will inform you that the user has initiated a purchase. You can trigger your custom purchase flow in response to this callback.

    The observerModeDidInitiateRestorePurchases(onStartRestore:onFinishRestore:) event will inform you that the user has initiated a restore. You can trigger your custom restore flow in response to this callback.

    Also, remember to invoke the following callbacks to notify AdaptyUI about the process of the purchase or restore. This is necessary for proper paywall behavior, such as showing the loader, among other things:

    CallbackDescription
    onStartPurchase()The callback should be invoked to notify AdaptyUI that the purchase is started.
    onFinishPurchase()The callback should be invoked to notify AdaptyUI that the purchase is finished.
    onStartRestore()The callback should be invoked to notify AdaptyUI that the restore is started.
    onFinishRestore()The callback should be invoked to notify AdaptyUI that the restore is finished.
  2. Create a paywall configuration object:

    do {
        let paywallConfiguration = try AdaptyUI.getPaywallConfiguration(
         forPaywall: <paywall object>, 
         observerModeResolver: <AdaptyObserverModeResolver>
         )
    } catch {
        // handle the error
    }

    Request parameters:

    ParameterPresenceDescription
    PaywallrequiredAn AdaptyPaywall object to obtain a controller for the desired paywall.
    ObserverModeResolverrequiredThe AdaptyObserverModeResolver object you’ve implemented in the previous step
  3. Initialize the visual paywall you want to display by using the .paywallController(for:products:viewConfiguration:delegate:) method:

    import AdaptyUI
    
    let visualPaywall = AdaptyUI.paywallController(
        with: <paywall configuration object>,
        delegate: <AdaptyPaywallControllerDelegate>
    )

Request parameters:

ParameterPresenceDescription
Paywall ConfigurationrequiredAn AdaptyUI.PaywallConfiguration object containing visual details of the paywall. Use the AdaptyUI.getPaywallConfiguration(forPaywall:locale:) method. Refer to Fetch Paywall Builder paywalls and their configuration topic for more details.
DelegaterequiredAn AdaptyPaywallControllerDelegate to listen to paywall events. Refer to Handling paywall events topic for more details.

Returns:

ObjectDescription
AdaptyPaywallControllerAn object, representing the requested paywall screen

After the object has been successfully created, you can display it like so:

present(visualPaywall, animated: true)
Warning

Don’t forget to Associate paywalls to purchase transactions. Otherwise, Adapty will not determine the source paywall of the purchase.