> For the complete documentation index, see [llms.txt](https://docs.habit.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.habit.io/sdk/ios-quick-start/usage/initialization.md).

# SDK Initialization

## Importing the SDK

First start by importing the HabitAnalytics module in the classes in which you're going to use it.

```swift
import HabitAnalytics
```

## Initialize SDK

In the `didFinishLaunchingWithOptions` method of your `AppDelegate`, call the initialize function of the SDK. The analyticsID and analyticsAPIKey parameters are mandatory and must be valid in order for the SDK to start. If you don't have credentials yet, check [here](/sdk/getting-started.md#requesting-an-analytics-id-and-api-key) for more info.

```swift
func initialize(analyticsID: String, analyticsAPIKey: String, configuration : Configuration?, externalID: String? = nil, completion: @escaping (_ status : HabitStatusCode) -> Void)
```

{% hint style="warning" %}
Please note that the analyticsID and analyticsAPIKey should not be hard-coded, unless for testing purposes. It is not secure and can lead to leaked credentials. In your app, ideally, load these credentials from a secure source such as a remote server, and store them securely on the device.
{% endhint %}

###

### Configuration

The configuration object sets the capabilities that are to be enabled in the SDK, as well as if the permissions for the capabilities have been granted to the App. By default all these values are false so make sure to enable the required ones.&#x20;

In the example below, the developer wishes to use the location capability but hasn't requested the user for permission yet. However, the permission can be updated at a later time after the permission has been requested. [See more info](/sdk/ios-quick-start/usage/configuration.md#permissions).&#x20;

```swift
let config = Configuration()
        
config.capabilities.bluetooth = true
config.capabilities.location = true
config.capabilities.motion = true
config.capabilities.ux_events = true

config.permissions.location =  false
config.permissions.bluetooth =  true
config.permissions.motion = true

config.uxEventsConfig.token = "<valid UX token>"
```

For more details on the configuration parameter please check the [Configuration](/sdk/ios-quick-start/usage/configuration.md) section of this guide.&#x20;

### Set External ID (Optional)

While user analytics information is anonymous, an external ID can be set so that the data can be correlated with a particular user.

{% hint style="success" %}
If an external ID is set, it should follow the best practices of user data consent.&#x20;
{% endhint %}

&#x20;To set an External user ID, it should either be provided upon initialization of the SDK or later by calling the appropriate function after a user performs a login in the app.&#x20;

#### Set External ID during initialization:

```swift
HabitAnalytics.shared.initialize(analyticsID: String, analyticsAPIKey: String, configuration: Configuration, externalID: String) { (statusCode) in
    debugPrint("\(statusCode) : \(HabitStatusCodes.getDescription(code: statusCode)")
}
```

#### Set External ID after initialization:

```swift
HabitAnalytics.shared.setExternalID(identifier: String) { (statusCode) in
    debugPrint("\(statusCode) : \(HabitStatusCodes.getDescription(code: statusCode)")
}
```

\
**HabitAnalyticsDelegate**
--------------------------

You can listen to the status changes of the SDK. In order to do that, implement the delegate HabitAnalyticsDelegate and the following method:

```swift
func HabitAnalyticsStatusChange(statusCode: HabitStatusCode) {
    print("\(statusCode) : \(HabitStatusCodes.getDescription(code: statusCode))")
}
```

## **Background fetch**

Using the background app refresh mechanism, the OS allows some minimal work to be done from time to time. Ensure you have this code on **didFinishLaunchingWithOptions** to set the minimum background fetch interval:

```swift
UIApplication.shared.setMinimumBackgroundFetchInterval(TimeInterval(1800))
```

Also add the following method that is executed when background fetch is triggered:

```swift
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

    HabitAnalytics.shared.handleBGFetch { (result) in
        completionHandler(result)
    }
}
```

## Logout

To logout a user, just call the corresponding method.&#x20;

{% hint style="warning" %}
Make sure to call the logout method when changing between users.
{% endhint %}

```swift
HabitAnalytics.shared.logout(completion: { (statusCode) in
    debugPrint("\(statusCode) : \(HabitStatusCodes.getDescription(code: statusCode)")
})
```
