Use this page with AI
Copy this into your coding assistant and add your request.
Read https://openiap.dev/docs/apis/init-connection and https://openiap.dev/llms.txt. Follow the reading instructions, detailed reference, and linked guides relevant to my task before making changes.
Inspect my existing project and reuse its framework and conventions. Ask me for missing product decisions. Implement the requested behavior and run the applicable checks.
Show the working result, the commands and actual test results, and any remaining limitations. Keep your explanation brief.
My request: [describe what customers should be able to do]initConnection
Initialize the store connection and wait for a successful result before starting store operations.
iOS: Receives promoted IAPs through PurchaseIntent.intents on iOS 16.4+ (and SKPaymentQueue only on iOS 15–16.3) and starts a Transaction.updates listener that drives the purchase event stream. Safe to call repeatedly. It also connects when Screen Time or MDM blocks purchases (AppStore.canMakePayments is false): reads and restore keep working, and requestPurchase rejects with iap-not-available. Apple docs. React Native and Expo can connect on demand on iOS, but an explicit call keeps listener setup and cross-platform gating predictable. Android: Starts BillingClient and waits for onBillingSetupFinished. React Native and Expo do not open it implicitly; wait for true or the hook's connected flag before another Play Billing call. Meta Horizon additionally requires a current foreground Activity; initialization fails with MissingCurrentActivity instead of falling back to an application context. Google docs.
Signature
func initConnection() async throws -> BoolParameters#
Pass an optional InitConnectionConfig — Android billing program flags. iOS ignores Android-specific fields.
enableBillingProgramAndroid(optional,BillingProgramAndroid) — Android. Enable a Play Billing program at connection time.EXTERNAL_CONTENT_LINKandEXTERNAL_OFFERrequire Billing 8.2.1+;EXTERNAL_PAYMENTSrequires Billing 8.3.0+ (Japan only);BILLING_CHOICEis available in OpenIAP 2.1.0 /openiap-google2.3.0 and requires Billing 9.1.0+.ALTERNATIVE_BILLING_ONLYuses a dedicated Play connection. Combining it with another program returnsdeveloper-errorbefore the active client is replaced.billingChoiceScreenTypeAndroid(optional,BillingChoiceScreenTypeAndroid) — OpenIAP 2.1.0 / openiap-google 2.3.0. Requires Play Billing 9.1.0+ and must match the Billing Choice renderer configured in Play Console. Defaults toGOOGLE_RENDERED; setDEVELOPER_RENDEREDwhen your app owns the choice screen.
On Google Play, calling it again changes the live connection:
- A config with a different program or renderer rebuilds the client; a matching config or no config keeps it.
- During a purchase the rebuild is refused with
developer-errorand the live client stays. A rebuild that fails leaves the store disconnected. - The program the app chose last survives a service drop or a failed connect, so a later call without a config reconnects with it. Only
endConnectionforgets it. - A config without a program returns to the standard client; programs queued with
enableBillingProgramAndroidstay. Flutter cannot pass an empty config, so it passesBillingProgramAndroid.Unspecified.
Meta Horizon has no billing programs: a connected client returns true and ignores the config.
Returns#
Promise<boolean> — true once the platform billing client is connected.
Throws#
When the platform billing client fails to initialize. The native Android module resolves false instead, and react-native-iap on iOS resolves false after sending the error to the purchase error listener, so check the result too. On iOS, kmp-iap throws ServiceTimeout when the App Store connection does not answer within 15 seconds.
Example
import OpenIAP
let connected = try await OpenIapModule.shared.initConnection()
precondition(connected, "Store connection failed")See InitConnectionConfig for the full list of supported config fields (enableBillingProgramAndroid, and billingChoiceScreenTypeAndroid).