> For the complete documentation index, see [llms.txt](https://docs.taptalk.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.taptalk.io/powertalk-chat-sdk-documentation/powertalk-ios/implement-application-delegate.md).

# Implement Application Delegate

To allow the iOS SDK to respond to the connection and state changes in your iOS client app, you have to implement all of our application delegate methods in your **`UIApplicationDelegate`** methods in appDelegate file.

After you initialize the TapTalk.io, you have to connect and implement these methods in your **UIApplicationDelegate** methods in **appDelegate** file to make sure TapTalk.io runs smoothly in your application.

| **TapTalk Application Delegate Metho**                                                                   | **Description**                                                                                      |
| -------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- |
| `application:didFinishLaunchingWithOptions`                                                              | Tells the delegate that the launch process is almost done and the app is almost ready to run.        |
| `applicationWillResignActive:`                                                                           | Tells the delegate that the app is about to become inactive.                                         |
| `applicationDidEnterBackground:`                                                                         | Tells the delegate that the app is now in the background.                                            |
| `applicationWillEnterForeground:`                                                                        | Tells the delegate that the app is about to enter the foreground.                                    |
| `applicationDidBecomeActive:`                                                                            | Tells the delegate that the app has become active.                                                   |
| `applicationWillTerminate:`                                                                              | Tells the delegate when the app is about to terminate.                                               |
| <p><code>application:</code></p><p><code>didRegisterForRemoteNotificationsWithDeviceToken:</code></p>    | Tells the delegate that the app successfully registered with Apple Push Notification service (APNs). |
| <p><code>application:didReceiveRemoteNotification:</code></p><p><code>fetchCompletionHandler:</code></p> | Tells the app that a remote notification arrived that indicates there is data to be fetched.         |
| `handleException:`                                                                                       | Tells the delegate when application throws exception.                                                |

{% tabs %}
{% tab title="Objective-C" %}
{% code title="AppDelegate.m" %}

```objective-c
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Implement TapTalk didFinishLaunchingWithOptions here    
    [[TapTalk sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];

    // Initialize TapTalk
    [[TapTalk sharedInstance] initWithAppKeyID:...];    
    
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Implement TapTalk applicationWillResignActive here
    [[TapTalk sharedInstance] applicationWillResignActive:application];
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Implement TapTalk applicationDidEnterBackground here
    [[TapTalk sharedInstance] applicationDidEnterBackground:application];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Implement TapTalk applicationWillEnterForeground here
    [[TapTalk sharedInstance] applicationWillEnterForeground:application];
}
  
- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Implement TapTalk applicationDidBecomeActive here
    [[TapTalk sharedInstance] applicationDidBecomeActive:application];
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Implement TapTalk applicationWillTerminate here
    [[TapTalk sharedInstance] applicationWillTerminate:application];
}
```

{% endcode %}
{% endtab %}

{% tab title="Swift" %}
{% code title="AppDelegate.swift" %}

```swift
import PowerTalk

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
    // Implement TapTalk didFinishLaunchingWithOptions here    
    TapTalk.sharedInstance().application(application, didFinishLaunchingWithOptions:launchOptions)

    // Initialize TapTalk
    TapTalk.sharedInstance().initWithAppKeyID(...)
        
    return true
}

func applicationWillResignActive(_ application: UIApplication) {
    // Implement TapTalk applicationWillResignActive here
    TapTalk.sharedInstance().applicationWillResignActive(application)
}

func applicationDidEnterBackground(_ application: UIApplication) {
    // Implement TapTalk applicationDidEnterBackground here
    TapTalk.sharedInstance().applicationDidEnterBackground(application)
}

func applicationWillEnterForeground(_ application: UIApplication) {
    // Implement TapTalk applicationWillEnterForeground here
    TapTalk.sharedInstance().applicationWillEnterForeground(application)
}

func applicationDidBecomeActive(_ application: UIApplication) {
    // Implement TapTalk applicationDidBecomeActive here
    TapTalk.sharedInstance().applicationDidBecomeActive(application)
}

func applicationWillTerminate(_ application: UIApplication) {
    // Implement TapTalk applicationWillTerminate here
    TapTalk.sharedInstance().applicationWillTerminate(application)
}
```

{% endcode %}
{% endtab %}
{% endtabs %}

{% hint style="warning" %}
**Note:** You have to implement `application:didRegisterForRemoteNotificationsWithDeviceToken` and `application:didReceiveRemoteNotification` to handle and receive notification from TapTalk.io.
{% endhint %}

{% tabs %}
{% tab title="Objective-C" %}
{% code title="AppDelegate.m" %}

```objective-c
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    // Implement TapTalk application:didRegisterForRemoteNotificationsWithDeviceToken here
    [[TapTalk sharedInstance] application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {
    // Implement TapTalk application:didReceiveRemoteNotification:fetchCompletionHandler here
    [[TapTalk sharedInstance] application:application didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
}
```

{% endcode %}
{% endtab %}

{% tab title="Swift" %}
{% code title="AppDelegate.swift" %}

```swift
import PowerTalk

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    // Implement TapTalk application:didRegisterForRemoteNotificationsWithDeviceToken here
    TapTalk.sharedInstance().application(application, didRegisterForRemoteNotificationsWithDeviceToken: deviceToken)
}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    // Implement TapTalk application:didReceiveRemoteNotification:fetchCompletionHandler here
    TapTalk.sharedInstance().application(application, didReceiveRemoteNotification: userInfo, fetchCompletionHandler: completionHandler)
}
```

{% endcode %}
{% endtab %}
{% endtabs %}

To register uncaught exception handler, call `NSSetUncaughtExceptionHandler(&handleExceptions)` in `application:didFinishLaunchingWithOptions:` callback and add delegate function `handleExceptions`.

{% tabs %}
{% tab title="Objective-C" %}
{% code title="AppDelegate.m" %}

```objective-c
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Implement TapTalk didFinishLaunchingWithOptions here    
    [[TapTalk sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];
    
    // Initialize TapTalk
    [[TapTalk sharedInstance] initWithAppKeyID:...];
    
    // Register Uncaught Exception Handler
    NSSetUncaughtExceptionHandler(&handleException);
    
    return YES;
}

// Add delegate function to handle exceptions
void handleException(NSException *exception) {
    // Implement TapTalk.io handleException method
    [[TapTalk sharedInstance] handleException:exception];
}
```

{% endcode %}
{% endtab %}

{% tab title="Swift" %}
{% code title="AppDelegate.swift" %}

```swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
    // Implement TapTalk didFinishLaunchingWithOptions here    
    TapTalk.sharedInstance().application(application, didFinishLaunchingWithOptions:launchOptions)
    
    // Initialize TapTalk
    TapTalk.sharedInstance().initWithAppKeyID(...)
    
    // Register Uncaught Exception Handler
    NSSetUncaughtExceptionHandler(handleException)
    
    return true
}

// Add delegate function to handle exceptions
let handleException: @convention(c) (NSException) -> Void = { exception in
    // Implement TapTalk.io handleException method
    TapTalk.sharedInstance().handle(exception)
}
```

{% endcode %}
{% endtab %}
{% endtabs %}

{% hint style="warning" %}
**Note:** Don't forget to register for exception handler inside `application:didFinishLaunchingWithOptions:` method and implement `handleExceptions` method in **appDelegate** class to make sure TapTalk.io able to handle exceptions.
{% endhint %}
