Skip to main content

Purpose

Present an Encore offer using a SwiftUI sheet controlled by view state. Use callbacks to react to the user’s decision.

Signature

public func encoreSheet(
  isPresented: Binding<Bool>,
  onGranted: @escaping (Entitlement) -> Void,
  onNotGranted: @escaping (NotGrantedReason) -> Void
) -> some View

Parameters

NameTypeDescription
isPresentedBinding<Bool>Controls the sheet’s presentation
onGrantedEntitlement -> VoidCalled when an offer is accepted. Use for side effects (analytics, UI). Entitlement state is also published via isActivePublisher
onNotGrantedNotGrantedReason -> VoidCalled when an offer is not granted (user declined or system reason). Proceed with the user’s original action here

Returns / State

Returns a modified view. Presentation is controlled by isPresented. Entitlement status changes are published via isActivePublisher and queryable with isActive.

Usage

import Encore
import SwiftUI

struct CancellationView: View {
  @State private var showEncoreOffer = false

  var body: some View {
    VStack {
      Button("Cancel Subscription") { showEncoreOffer = true }
    }
    .encoreSheet(
      isPresented: $showEncoreOffer,
      onGranted: { entitlement in
        // Keep subscription
        print("Accepted: \(entitlement)")
      },
      onNotGranted: { reason in
        // Proceed with cancellation
        print("Not granted: \(reason)")
        proceedWithCancellation()
      }
    )
  }
}