iOS – Configure Adapty SDK

Learn how to import Adapty iOS SDK in your app, configure it, and set up logging

You only need to configure the Adapty SDK once, typically early in your application lifecycle:

// In your AppDelegate class:

let configurationBuilder =
	Adapty.Configuration
		.Builder(withAPIKey: "PUBLIC_SDK_KEY")
		.with(observerMode: false)
		.with(customerUserId: "YOUR_USER_ID")
		.with(idfaCollectionDisabled: false)
		.with(ipAddressCollectionDisabled: false)

Adapty.activate(with: configurationBuilder) { error in
  // handle the error
}
import Adapty

@main
struct SampleApp: App {
    init() 
      let configurationBuilder =
        Adapty.Configuration
          .Builder(withAPIKey: "PUBLIC_SDK_KEY")
          .with(observerMode: false)
          .with(customerUserId: "YOUR_USER_ID")
          .with(idfaCollectionDisabled: false)
          .with(ipAddressCollectionDisabled: false)

        Adapty.activate(with: configurationBuilder) { error in
          // handle the error
        }
    }

    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

Parameters:

ParameterPresenceDescription
PUBLIC_SDK_KEYrequiredThe key you can find in the Public SDK key field of your app settings in Adapty: App settings-> General tab -> API keys subsection
observerModeoptionalA boolean value controlling Observer mode. Turn it on if you handle purchases and subscription status yourself and use Adapty for sending subscription events and analytics.
The default value is false.

🚧 When running in Observer mode, Adapty SDK won't close any transactions, so make sure you're handling it.
customerUserIdoptionalAn identifier of the user in your system. We send it in subscription and analytical events, to attribute events to the right profile. You can also find customers by customerUserId in the Profiles and Segments menu.
idfaCollectionDisabledoptionalSet to true to disable IDFA collection and sharing.
the user IP address sharing.
The default value is false.
For more details on IDFA collection, refer to the Analytics integration section.
ipAddressCollectionDisabledoptionalSet to true to disable user IP address collection and sharing.
The default value is false.

📘

Make sure you use the Public SDK key for Adapty initialization, the Secret key should be used for server-side API only.

Also, note that SDK keys are unique for every app, so if you have multiple apps make sure you choose the right one.

Logging system

Adapty logs errors and other important information to help you understand what is going on. There are four levels available:

  1. .error: only errors will be logged
  2. .warn: messages from the SDK that do not cause critical errors, but are worth paying attention to
  3. .info: various information messages, such as those that log the lifecycle of various modules
  4. .verbose: any additional information that may be useful during debugging, such as function calls, API queries, etc.

You can set logLevel at any time in the application's lifecycle, but we recommend that you do this before calling .activate().

Adapty.logLevel = .verbose

Overriding logger handler

If you for some reason need to send messages from Adapty to your system or save them to a file, you can override the default behavior:

Adapty.setLogHandler { level, message in
    writeToLocalFile("Adapty \(level): \(message)")
}

Redirect the logging system messages

If you for some reason need to send messages from Adapty to your system or save them to a file, you can override the default behavior:

Adapty.setLogHandler { level, message in
    writeToLocalFile("Adapty \(level): \(message)")
}