# Quick Start

{% hint style="info" %}
Check our public [sample application](https://bitbucket.org/dolphin-technologies/move-ios-sample/src/master/) for an actual implementation of the snippets below and how they can be utilized.&#x20;
{% endhint %}

## Overall Code

A quick start snippet will look something like this in your app's [`willFinishLaunchWithOptions`](https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623032-application):

```swift
import MoveSDK

func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
     
    MoveSDK.shared.setSDKStateListener { state in
        switch state {
        case .uninitialized:
            /* SDK uninitialized*/
            break
        case .ready:
            /* SDK initialized and ready to start the service*/
            MoveSDK.shared.startAutomaticDetection()
        case .running:
            /* SDK Started Detection*/
            break
        case let .error(error):
            switch error {
            /* Hanlde SDK State Errors*/
            default: break
            }
        }
    }
 
    let sdkConfig = MoveConfig(timelineDetectionService: 
                    [.walking, .driving, .bicycle, .places, .publicTransport], 
                    drivingServices: [.dfd, .behaviour], 
                    otherServices: [.poi])
 
    MoveSDK.shared.initialize(auth: auth, config: sdkConfig, launchOptions: launchOptions) { initError in
        if let error = initError {
            switch error {
            /* Hanlde SDK Initialize Error*/
            default: break
            }
        }
    }
}
```

{% hint style="warning" %}
This snippet assumes that the required configurations and permissions are already setup to work. Check [permissions handling](https://docs.movesdk.com/move-platform/move-sdk-1.x/appendix/ios/permission-handling#required-permissions) section for more details.
{% endhint %}

## Breakdown

Let us break down the overall code block into the following:&#x20;

### SDK Authorization

The [MoveAuth](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/moveauth) will look something like this:

```swift
let auth = MoveAuth(userToken: "",refreshToken: "" contractID: "", productID: "" )
```

If the provided [MoveAuth](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/moveauth) was invalid, the SDK will not initialize and complete with [MoveConfigurationError](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/moveconfigurationerror)`.authInvalid`. Check [Initialization](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/api-interface/ios/intialization) for more details about possible outcomes.

{% hint style="warning" %}

### Authentication Expiry

The host app is expected to monitor [MoveAuthState](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/moveauthstate) updates via`authStateChangeListener`([iOS](https://docs.movesdk.com/move-platform/move-sdk-1.x/api-interface/ios/services#set-sdk-auth-state-listener)) API and handle those changes accordingly.

Check [Authentication updates and expiry](https://docs.movesdk.com/move-platform/move-sdk-1.x/models/moveauthstate#authentication-updates-and-expiry) for more details about authentication expiry and renewal.
{% endhint %}

### SDK Configuration

[MoveConfig](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/moveauth) allows host apps to configure which of the licensed Move services should be enabled. It could be based on each user preference or set from a remote server.

{% hint style="danger" %}
**iOS System Permissions**

Based on the passed [MoveConfigs](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/moveauth) on[`initialization`](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/api-interface/ios/intialization) , the SDK determines the required permissions to activate the requested services.\
\
The host app **must** verify that all the permissions required for the passed configs are granted.\
\
In case the required permissions were missing when calling SDK [initialization](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/api-interface/ios/intialization) API, the [MoveSDKState](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/movedevicestatus) will transit to `permissionMissing`error [MoveSDKState](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/movedevicestatus) other than `ready`[MoveSDKState](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/movedevicestatus).

Check [permission handling](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/appendix/ios/permission-handling) for details about permissions required for each service.
{% endhint %}

The [MoveConfig](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/moveauth) will look something like this:

```swift
let sdkConfig = MoveConfig(
                timelineDetectionService: [.walking, .driving, .bicycle, .places, 
                .publicTransport],
                drivingServices: [.dfd, .behaviour], 
                otherServices: [.poi])
```

### SDK State

The host app is expected to set its [`SDKStateListener`](https://docs.movesdk.com/move-platform/move-sdk-1.x/api-interface/ios/services#set-sdk-state-listener) **before** initializing the SDK to intercept the [MoveSDKState](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/movestate) changes caused by calling the [`initialize`](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/api-interface/ios/intialization) API.

The provided block could then start the SDK when [MoveSDKState](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/movestate) is `ready` or handle errors if occurred. The provided block could look something like this:&#x20;

{% hint style="info" %}
Only calling [`startAutomaticDetection`](https://docs.movesdk.com/move-platform/move-sdk-1.x/api-interface/ios/services#start-automatic-detection)API will transit [MoveSDKState](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/models/movestate) is `ready state.`
{% endhint %}

```swift
MoveSDK.shared.setSDKStateListener { state in
    switch state {
    case .uninitialized:
        /* SDK uninitialized*/
        break
    case .ready:
        /* SDK initialised and ready to start the service*/
        MoveSDK.shared.startAutomaticDetection()
    case .running:
        /* SDK Started Detection*/
        break
    case let .error(error):
        switch error {
        /* Hanlde SDK State Errors*/
        default: break
        }
    }
}
```

### SDK Initialization&#x20;

{% hint style="warning" %}
The[`initialization`](https://docs.movesdk.com/move-platform/move-sdk-1.x/sdk/api-interface/ios/intialization)API must be executed before[`didFinishLaunchingWithOptions`](https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1622921-application)returns. We recommend calling it in[`willFinishLaunchingWithOptions`](https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623032-application) .\
\
Exceptions might apply, where the SDK is not initialized on app launch.  First initialization is a good example, where the app would only initialize the SDK after onboarding the user and requesting permissions. \
\
Check[ Initialization Timing](https://docs.movesdk.com/move-platform/move-sdk-1.x/api-interface/ios/intialization#timing) for more details about the importance of adequately initializing the SDK.
{% endhint %}

```swift
MoveSDK.shared.initialize(auth: auth, config: sdkConfig, launchOptions: launchOptions) { initError in
    if let error = initError {
        switch error {
        /* Hanlde SDK Initialization MoveConfigurationError*/
        default: break
        }
    }
}
```
