> ## Documentation Index
> Fetch the complete documentation index at: https://moengage-sdk-docs.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# SDK Initialization

# Initializing MoEngage SDK

First, go to `Build Settings` of your `App Target` and ensure that **DEBUG** Preprocessor Macro is defined in `Debug` section as shown in the below image, if not present then add the same by entering `DEBUG=1` in `Debug` section:

<img src="https://mintcdn.com/moengage-sdk-docs/JWiTYHgM3sZmk2vD/images/sdkini.png?fit=max&auto=format&n=JWiTYHgM3sZmk2vD&q=85&s=9dae30122d44431add7bdcd7113e856e" alt="Sdkini" width="2148" height="782" data-path="images/sdkini.png" />

**For Swift Project**, In `App Target` `Build Settings` make sure **-DDEBUG** is added to `Debug` section in the `Other Swift Flags` as described in the image:

<img src="https://mintcdn.com/moengage-sdk-docs/JWiTYHgM3sZmk2vD/images/sdkini2.png?fit=max&auto=format&n=JWiTYHgM3sZmk2vD&q=85&s=90d2a47fd59c243369c8f04a8e3165f5" alt="Sdkini2" width="2146" height="652" data-path="images/sdkini2.png" />

* in to your MoEngage account, go to **Settings** in the left panel of the dashboard. Under General Settings, you will find your **Workspace ID**. Provide this Workspace ID along with the Datacenter while initializing the SDK with MoEngageSDKConfig instance . Use[***initializeDefaultTestInstance(\_:)***](https://moengage.github.io/ios-api-reference/Classes/MoEngage.html#/c:@M@MoEngageSDK@objc\(cs\)MoEngage\(im\)initializeDefaultTestInstance:)  and [***initializeDefaultLiveInstance(\_:)***](https://moengage.github.io/ios-api-reference/Classes/MoEngage.html#/c:@M@MoEngageSDK@objc\(cs\)MoEngage\(im\)initializeDefaultLiveInstance:) methods as shown below.

  <CodeGroup>
    ```Swift Swift wrap theme={"theme":{"light":"github-light","dark":"github-dark"}}
    import MoEngageSDK
     func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
      
          //Add your MoEngage Workspace ID and Data center.
          let sdkConfig = MoEngageSDKConfig(appId: "YOUR WorkspaceID", dataCenter: DATA_CENTER);

          // MoEngage SDK Initialization
          // Separate initialization methods for Dev and Prod initializations
          #if DEBUG
              MoEngage.sharedInstance.initializeDefaultTestInstance(sdkConfig)
          #else
             MoEngage.sharedInstance.initializeDefaultLiveInstance(sdkConfig)
          #endif
     
      //Rest of the implementation of method
      //-------
      return true
    }
    ```

    ```objective-c Objective C wrap theme={"theme":{"light":"github-light","dark":"github-dark"}}
    @import MoEngageSDK;
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
         //TODO: Add your MoEngage Workspace ID and Data center.
         MoEngageSDKConfig* sdkConfig = [[MoEngageSDKConfig alloc] initWithAppId:@"YOUR Workspace ID" dataCenter: DATA_CENTER];
        
    // MoEngage SDK Initialization
        // Separate initialization methods for Dev and Prod initializations
        #ifdef DEBUG
             [[MoEngage sharedInstance] initializeDefaultTestInstance:sdkConfig];
        #else
             [[MoEngage sharedInstance] initializeDefaultLiveInstance:sdkConfig];
        #endif
        
      //Rest of the implementation of method
      //-------
    }
    ```
  </CodeGroup>
* In your MoEngage account, if your [portfolio](https://help.moengage.com/hc/en-us/articles/40394603054100-Portfolio) is configured with multiple projects, Provide the respective Project ID along with the Datacenter while initializing the SDK with MoEngageSDKConfig instance . Use [initializeDefaultTestInstance(\_:)](https://moengage.github.io/ios-api-reference/Classes/MoEngage.html#/c:@M@MoEngageSDK@objc\(cs\)MoEngage\(im\)initializeDefaultTestInstance:) and[ initializeDefaultLiveInstance(\_:)](https://moengage.github.io/ios-api-reference/Classes/MoEngage.html#/c:@M@MoEngageSDK@objc\(cs\)MoEngage\(im\)initializeDefaultLiveInstance:) methods as shown below:

  <CodeGroup>
    ```swift Swift wrap theme={"theme":{"light":"github-light","dark":"github-dark"}}
    let sdkConfig = MoEngageSDKConfig(appId: "YOUR WorkspaceID", dataCenter: DATA_CENTER)
    sdkConfig.projectConfig = MoEngageProjectConfig(projectID: "YOUR ProjectID")
    #if DEBUG
    MoEngage.sharedInstance.initializeDefaultTestInstance(sdkConfig)
    #else
    MoEngage.sharedInstance.initializeDefaultLiveInstance(sdkConfig)
    #endif
    ```

    ```objective-c Objective C wrap theme={"theme":{"light":"github-light","dark":"github-dark"}}
    MoEngageSDKConfig* sdkConfig = [[MoEngageSDKConfig alloc] initWithAppId:@"YOUR Workspace ID" dataCenter: DATA_CENTER];
    sdkConfig.projectConfig = [[MoEngageProjectConfig alloc] initWithProjectID:@"YOUR Project ID"];
    #ifdef DEBUG
    [[MoEngage sharedInstance] initializeDefaultTestInstance:sdkConfig];
    #else
    [[MoEngage sharedInstance] initializeDefaultLiveInstance:sdkConfig];
    #endif
    ```
  </CodeGroup>

Following details of the different data centers you need to set based on the dashboard hosts

| Data Center                | Dashboard host            |
| :------------------------- | :------------------------ |
| DataCenter.DATA\_CENTER\_1 | dashboard-01.moengage.com |
| DataCenter.DATA\_CENTER\_2 | dashboard-02.moengage.com |
| DataCenter.DATA\_CENTER\_3 | dashboard-03.moengage.com |
| DataCenter.DATA\_CENTER\_4 | dashboard-04.moengage.com |
| DataCenter.DATA\_CENTER\_5 | dashboard-05.moengage.com |
| DataCenter.DATA\_CENTER\_6 | dashboard-06.moengage.com |

For more information about the detailed list of possible configurations, refer to the [API reference](https://moengage.github.io/ios-api-reference/Classes/MoEngageSDKConfig.html).

<Note>
  **Note**

  ***data\_center\_06*** is available from MoEngage-iOS-SDK version 9.17.3 onwards
</Note>

<Warning>
  **Important**

  Make sure to call the initialization method in `applicationDidFinishLaunching(_:)` method. In case if you are initializing the SDK at a later stage and not at launch then you will have to call all the notification related methods instead of just relying on [AppDelegate Swizzling](/developer-guide/ios-sdk/push/basic/ios-push-integration-tutorial#app-delegate-method-swizzling).
</Warning>

# Test/Live Environment

As mentioned above while initializing the build, MoEngage SDK makes use of the `DEBUG` preprocessor macro to decide whether the build is meant for TEST/LIVE Environment. Therefore, you will have to take care of the same while generating the build and make sure that the **Build Configuration** of the App's target is set as mentioned below:

* For **Development** Build: Build Configuration should be set to **Debug** (Data will be tracked in **TEST** Environment)
* For **AdHoc Build/App Store** Build: Build Configuration should be set to **Release** (Data will be tracked in **LIVE** Environment)

<Warning>
  **What if Build Configuration is not set correctly?**

  If the build configuration is not set correctly following might happen:

  * You will see the data from the development build in LIVE environment
  * **OR** data from AdHoc/Production Build in TEST environment
  * You will get **Bad Device Token** error while trying to send a push notification to the device
</Warning>

# How to set Build Configuration?

## Build Configuration on Running the app from Xcode:

Whenever you run the app directly from Xcode without archiving, make sure the build configuration of **Run mode** of the App Target in Edit Scheme is set to **Debug**(set by default settings). Doing this will make sure data is tracked in TEST Environment.

<img src="https://mintcdn.com/moengage-sdk-docs/JWiTYHgM3sZmk2vD/images/sdkini3.png?fit=max&auto=format&n=JWiTYHgM3sZmk2vD&q=85&s=58ca8c1af9a5c9012b41025533851111" alt="Sdkini3" width="2072" height="1218" data-path="images/sdkini3.png" />

## Build Configuration on Exporting the build:

While exporting the build make sure you set the correct `Build Configuration`. By default for Archive section in `Edit Scheme` the `Build Configuration` will be set to `Release`, but for a development build make sure its changed to `Debug` before exporting the build. To set the **Build Configuration** of the build in your Xcode project go to **App Target > Edit Scheme > Archive > Build Configuration** and set the configuration to **Debug/Release**(depending on the type of build). Refer to the image as described:

<img src="https://mintcdn.com/moengage-sdk-docs/JWiTYHgM3sZmk2vD/images/sdkini4.png?fit=max&auto=format&n=JWiTYHgM3sZmk2vD&q=85&s=f87b07c2f5c30b29cd657533b2b7ef66" alt="Sdkini4" width="2062" height="1212" data-path="images/sdkini4.png" />

# Switching Environment in Dashboard

In the MoEngage dashboard you can switch between [test and live environment](https://help.moengage.com/hc/en-us/articles/210845683-LIVE-TEST-environments) for your app.

**TEST** Environment is used for all the development and testing-related uses and **LIVE** environment is used for running all the campaigns for AppStore Builds for your app's user base.
