# iOS SDK

## iOS Application Delegate

The **ForterSDK** needs to be **initialized from the Application Delegate**. As the primary root of an iOS app, the app delegate object is created early in your app's launch cycle and the initialization of the Forter SDK within the app delegate will ensure that it loads correctly when the app is active. For more information on the app delegate, please see the [Offical iOS developer documentation](https://developer.apple.com/documentation/uikit/uiapplicationdelegate "Offical iOS developer documentation")

## [1. Initialize from the Application Delegate](https://docs.forter.com/mobile-sdks/ios-application-delegate#1-initialize-from-the-application-delegate) 1. Initialize from the Application Delegate

The **ForterSDK** needs to be **initialized from the Application Delegate**.

1. Import the SDK using:
   - import ForterSDK for Swift
   - #import <ForterSDK/ForterSDK.h> for Obj-C.
2. Retrieve your site ID from the [Credentials](https://portal.forter.com/app/integration/credentials/ "Credentials") section of Forter Portal and make sure to toggle to the correct sandbox or production environment.
3. **Initialize the ForterSDK** with your site_id by adding the following line to your application delegate's [:didFinishLaunchingWithOptions](https://developer.apple.com/reference/uikit/uiapplicationdelegate/1622921-application ":didFinishLaunchingWithOptions") method:

**Swift**
   ```swift
   ForterSDK.setup(withSiteId:"your_site_id")
   ```
   
   **Objective C**
   ```objc
   [ForterSDK setupWithSiteId:@"your_site_id"];
   ```

## [2. Add Callbacks from the Application Delegate](https://docs.forter.com/mobile-sdks/ios-application-delegate#2-add-callbacks-from-the-application-delegate) 2. Add Callbacks from the Application Delegate

When receiving messages to your application delegate please call the corresponding methods in ForterSDK's delegate, FTRSDKApplicationDelegate.

For example, upon receiving a call to [applicationDidBecomeActive(_:)](https://developer.apple.com/documentation/uikit/uiapplicationdelegate/applicationdidbecomeactive(_:) "applicationDidBecomeActive(_:")

Your delegate should look like the following:

**Swift**
```swift
func applicationDidBecomeActive(application: UIApplication){
   FTRSDKApplicationDelegate.sharedInstance().applicationDidBecomeActive(application)
}
```

**Objective C**
```objc
-(void)applicationDidBecomeActive:(UIApplication *)application {
   [[FTRSDKApplicationDelegate sharedInstance] applicationDidBecomeActive: application];
}
```

Please note that if you’re using [Scenes](https://developer.apple.com/documentation/uikit/scenes "Scenes"), UIKit will not call applicationDidBecomeActive.

To ensure applicationDidBecomeActive is still forwarded to ForterSDK in scene-based apps, listen for [didBecomeActiveNotification](https://developer.apple.com/documentation/uikit/uiapplication/didbecomeactivenotification "didBecomeActiveNotification") instead:

**Swift**
```swift
NotificationCenter.default.addObserver(
   self,
   selector: #selector(handleAppDidBecomeActive),
   name: UIApplication.didBecomeActiveNotification,
   object: nil
)

@objc func handleAppDidBecomeActive(){
   FTRSDKApplicationDelegate.sharedInstance().applicationDidBecomeActive(UIApplication.shared)
}
```

**Objective C**
```objc
[[NSNotificationCenter defaultCenter] addObserver:self
   selector:@selector(handleAppDidBecomeActive)
   name:UIApplicationDidBecomeActiveNotification
   object:nil];

-(void)handleAppDidBecomeActive {
   [[FTRSDKApplicationDelegate sharedInstance] applicationDidBecomeActive:[UIApplication sharedApplication]];
}
```

Please make sure to call ALL the AppDelegate callbacks.
