Setting up delegates for links

Introduction

Click delegates are custom handlers that let you intercept and manage the SDK’s behavior when users tap clickable weblinks in a conversation. By default, the SDK automatically handles taps on weblinks by opening the Safari browser.

Use cases

Here are some common ways you can use click delegates to enhance the user experience in your app:

  • Deep linking: Intercept URLs to navigate users to specific screens.
  • Analytics: Log events when users click on any URL to track engagement.
  • Custom actions: Define custom behaviors, such as opening weblinks in a custom in-app browser.

Implementing click delegates

To customize how URL clicks are handled, you need to implement a MessagingDelegate and override the messaging(:shouldHandleURL:from: function. This function is triggered every time a user clicks a URL in the conversation.

Within shouldHandleURL, you decide whether the SDK should handle the URL or if your app will take over.

  • To let the SDK handle it: Return true. The SDK will handle the URL by launching Safari browser. You can still perform custom actions before returning.
  • To handle it yourself: Perform your custom action, then return false. This prevents the SDK from handling the click and gives your app complete control over the action.

The shouldHandleURL function provides the following parameters:

  • url: The URL that was clicked.
  • urlSource: An enum that tells you where the URL was clicked in the UI, such as a text message or a carousel.

Here's how to implement the MessagingDelegate in Swift and Objective-C:

Swift

  1. Set the delegate for Messaging.
Messaging.delegate = self
  1. Handle the URL in the delegate method messaging(_ messaging: Messaging, shouldHandleURL url: URL, from source: URLSource) -> Bool.
func messaging(_ messaging: Messaging, shouldHandleURL url: URL, from source: URLSource) -> Bool {    // Your custom action...
    // Return false to prevent the SDK from handling the URL automatically    // Return true to allow the SDK to handle the URL automatically, even    // if you have done something custom    false}

Objective-C

  1. Set the delegate for ZDKMessaging.
ZDKMessaging.delegate = self
  1. Handle the URL in the delegate method - (BOOL)messaging:(ZDKMessaging * _Nonnull)messaging shouldHandleURL:(NSURL * _Nonnull)URL from:(enum ZDKURLSource)source.
- (BOOL)messaging:(ZDKMessaging * _Nonnull)messaging shouldHandleURL:(NSURL * _Nonnull)URL from:(enum ZDKURLSource)source {    // Your custom action...
    // Return false to prevent the SDK from handling the URL automatically    // Return true to allow the SDK to handle the URL automatically, even    // if you have done something custom    return NO;}

For a working example, see the Zendesk SDK Demo App github

FAQ

Q: What happens if no MessagingDelegate is implemented?
A:If you don't set a delegate, the SDK will handle all URL clicks automatically by opening them in the Safari browser.

Q: Is it possible to handle some URLs and let the SDK handle others?
A: Yes. Inside shouldHandleURL, you can add conditional logic. For URLs you want to handle yourself, perform your action and return false. For all other URLs, return true to let the SDK handle them.

// Example: Handle only specific URLs from text and image sourcesfunc messaging(_ messaging: Messaging, shouldHandleURL url: URL, from source: URLSource) -> Bool {    switch urlSource {    case .text, .image:        if url.absoluteString.hasPrefix("https://www.zendesk.com/") {            // Handle it yourself for text and image sources only            return false        } else {            // Let SDK handle other URLs from text and image sources            return true        }    default:        // Let SDK handle all other URL sources by default        return true    }}