iOS promotedProductListenerIOS

Fired when a user clicks on a promoted in-app purchase in the App Store.

Listener Setup

swift
func promotedProductListenerIOS(
    _ listener: @escaping (String) -> Void
) -> Subscription

Registers a listener for App Store promoted product events. OpenIAP uses PurchaseIntent.intents on iOS 16.4+ and the StoreKit 1 observer only on iOS 15–16.3, so both mechanisms never run together.

swift
import OpenIap

let subscription = OpenIapModule.shared.promotedProductListenerIOS { productId in
    Task {
        print("Promoted product tapped: \(productId)")

        do {
            let result = try await OpenIapModule.shared.fetchProducts(
                ProductRequest(skus: [productId], type: .all)
            )
            guard case let .all(items) = result,
                  let item = items?.first else { return }

            switch item {
            case let .product(product):
                guard await showPurchaseConfirmation(product) else { return }
                try await OpenIapModule.shared.requestPurchase(
                    RequestPurchaseProps(
                        request: .purchase(
                            RequestPurchasePropsByPlatforms(
                                apple: RequestPurchaseIosProps(sku: productId)
                            )
                        )
                    )
                )
            case let .productSubscription(subscription):
                guard await showPurchaseConfirmation(subscription) else { return }
                try await OpenIapModule.shared.requestPurchase(
                    RequestPurchaseProps(
                        request: .subscription(
                            RequestSubscriptionPropsByPlatforms(
                                apple: RequestSubscriptionIosProps(sku: productId)
                            )
                        )
                    )
                )
            }
        } catch {
            print("Promoted purchase failed: \(error.localizedDescription)")
        }
    }
}

// Keep the token in the owning object, then call this on teardown.
func stopListeningForPromotedProducts() {
    OpenIapModule.shared.removeListener(subscription)
}

Handling Promoted Products

  1. Receive product SKU via listener
  2. Fetch product details using fetchProducts
  3. Display product information to user
  4. Call requestPurchase with the received SKU if user confirms

Also check getPromotedProductIOS on app launch for pending promoted products.