# SDK Delegates

{% hint style="danger" %}
We recommend including all SDK delegates within your application's `AppDelegate` class to ensure proper handling of all events.
{% endhint %}

## SDK Lifecycle Delegates

These delegates can be implemented to observe SDK loading and exiting.

You must inform the SDK what class is going to implement this protocol. This can be done using the `setSDKLifecycleDelegate` method on the SDK:

{% tabs %}
{% tab title="Swift" %}

```swift
PushSDK.setSDKLifecycleDelegate(self)
```

{% endtab %}

{% tab title="Objective-C" %}

```objectivec
[PushSDK setSDKLifecycleDelegate:self];
```

{% endtab %}
{% endtabs %}

The following SDK Lifecycle delegates are available:

### Finished Loading

This delegate is called when the SDK finishes its initialization and is ready for interaction.

{% tabs %}
{% tab title="Swift" %}

```swift
func pushSDK(didFinishLoading configuration: PNApplicationConfig, withNotificationSettings settings: UNNotificationSettings) {
    // Add code to execute after SDK finishes loading
}
```

{% endtab %}

{% tab title="Objective-C" %}

```objectivec
- (void)pushSDKDidFinishLoading:(PNApplicationConfig *)configuration withNotificationSettings:(UNNotificationSettings *)settings {
    // Add code to execute after SDK finishes loading
}
```

{% endtab %}
{% endtabs %}

## Permission Lifecycle Delegates

These delegates can be implemented to observe a user's permission authorization changes.

You must inform the SDK what class is going to implement this protocol. This can be done using the `setPermissionLifecycleDelegate` method on the SDK:

{% tabs %}
{% tab title="Swift" %}

```swift
PushSDK.setPermissionLifecycleDelegate(self)
```

{% endtab %}

{% tab title="Objective-C" %}

```objectivec
[PushSDK setPermissionLifecycleDelegate:self];
```

{% endtab %}
{% endtabs %}

The following Permission Lifecycle delegates are available:

### Permission Status Changed

This delegate is called when a user's permission status/authorization has changed.

The `status` variable represents the user's **new** permission status using the [UNAuthorizationStatus](https://developer.apple.com/documentation/usernotifications/unauthorizationstatus) enum.

{% tabs %}
{% tab title="Swift" %}

```swift
func pushSDK(didReceivePermissionStatusChange status: UNAuthorizationStatus, withSettings settings: UNNotificationSettings) {
    // Add code to execute after a user's notification permission has changed
}
```

{% endtab %}

{% tab title="Objective-C" %}

```objectivec
- (void)pushSDKDidReceivePermissionStatusChange:(UNAuthorizationStatus)status withSettings:(UNNotificationSettings *)settings {
    // Add code to execute after a user's notification permission has changed
}

```

{% endtab %}
{% endtabs %}

## Notification Lifecycle Delegates

These delegates can be implemented to observe events like impressions, opens, and other interactions with notifications.

You must inform the SDK what class is going to implement this protocol. This can be done using the `setNotificationLifecycleDelegate` method on the SDK:

{% tabs %}
{% tab title="Swift" %}

```swift
PushSDK.setNotificationLifecycleDelegate(self)
```

{% endtab %}

{% tab title="Objective-C" %}

```objectivec
[PushSDK setPermissionLifecycleDelegate:self];
```

{% endtab %}
{% endtabs %}

The following Notification Lifecycle delegates are available:

### Notification Received

This delegate is called when a user receives a notification

{% tabs %}
{% tab title="Swift" %}

```swift
func pushSDK(didReceiveNotification notification: PNNotification)
    // Add code to execute after the subscriber's device receives a notification
}
```

{% endtab %}

{% tab title="Objective-C" %}

```objectivec
- (void)pushSDKDidReceiveNotification:(PNNotification *)notification {
    // Add code to execute after the subscriber's device receives a notification
}
```

{% endtab %}
{% endtabs %}

### Notification Opened With URI/URL Destination

This delegate is called when a notification is opened that has a landing URL attached. The subscriber should be navigated to the view that represents the URL.

This method expects a Boolean response. If the response is `true` then the SDK will perform no navigation action for this notification open. If the response is `false` then the SDK will attempt to navigate the subscriber to the destination URL by using the `open` method.

{% tabs %}
{% tab title="Swift" %}

```swift
func pushSDK(didReceiveNotificationDestination destination: String, withInteraction interaction: PNNotificationInteraction) -> Bool {
    // Navigate to a view within your application using the destination (the Landing URL)
        
    // Return true to inform the SDK that it should not navigate
    return true
}
```

{% endtab %}

{% tab title="Objective-C" %}

```objectivec
- (BOOL)pushSDKDidReceiveNotificationDestination:(NSString *)destination withInteraction:(PNNotificationInteraction *)interaction {
    // Redirect to a view within your application using the destination (the Landing URL)
    
    // Return YES to inform the SDK that it should not redirect
    return YES;
}
```

{% endtab %}
{% endtabs %}

See the [Deep Linking](/integration/implementation-steps/apple-ios/sdk-swift-obj-c/deep-linking.md) documentation for a complete example.

## App Message Lifecycle Delegates

These delegates can be implemented to observe events like impressions, opens, and other interactions with app messages.

You must inform the SDK what class is going to implement this protocol. This can be done using the `setAppMessageLifecycleDelegate` method on the SDK:

{% tabs %}
{% tab title="Swift" %}

```swift
PushSDK.setAppMessageLifecycleDelegate(self)
```

{% endtab %}

{% tab title="Objective-C" %}

```objectivec
[PushSDK setAppMessageLifecycleDelegate:self];
```

{% endtab %}
{% endtabs %}

The following App Message Lifecycle delegates are available:

### App Message Will Present

This delegate is called when an App Message is presented to a user.

{% tabs %}
{% tab title="Swift" %}

```swift
func pushSDK(willPresentAppMessage appMessage: PNAppMessage) {
    // Add code to execute when a user's device presents an app message
}
```

{% endtab %}

{% tab title="Objective-C" %}

```objectivec
- (void)pushSDKWillPresentAppMessage:(PNAppMessage *)appMessage {
    // Add code to execute when a user's device presents an app message
}
```

{% endtab %}
{% endtabs %}

### App Message Received User Interaction

This delegate is called when a user interacts with an App Message.

This method expects a Boolean response. If the response is `true` then the SDK will not handle the interaction or attempt to perform navigation when the interaction is an Open URL action. If the response is `false` then the SDK will process the interaction and if the interaction is an Open URL action will attempt to navigate the user to the destination URL by using the `open` method.

{% tabs %}
{% tab title="Swift" %}

```swift
func pushSDK(didReceiveAppMessageInteraction interaction: PNAppMessageInteraction, fromAppMessage appMessage: PNAppMessage) -> Bool {
    // Add code to execute when a user interacts with an app message
    
    // Return true to inform the SDK that it should not handle the interaction
    return true
}
```

{% endtab %}

{% tab title="Objective-C" %}

```objectivec
- (BOOL)pushSDKDidReceiveAppMessageInteraction:(PNAppMessageInteraction *)interaction fromAppMessage:(PNAppMessage *)appMessage {
    // Add code to execute when a user interacts with an app message

    // Return YES to inform the SDK that it should not handle the interaction
    return YES;
}
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://documentation.pushly.com/integration/implementation-steps/apple-ios/sdk-swift-obj-c/sdk-delegates.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
