# Deep Linking

### Deep Linking

To redirect users to a specific page or view in your app via push notifications with custom action buttons, you can use deep links. Follow these steps to set up deep linking in your app:

1. **Set Up a URL Scheme**\
   In your Xcode project, define a custom URL scheme (`your_deeplink_scheme://`).
2. **Configure Info.plist**
   * Open your **Info.plist** file.
   * Add a new key: `URL types`**.**  Xcode will create an array with a dictionary called `Item 0`.
   * Inside `Item 0`, add:
     * `URL identifier` > Set it to your custom scheme.
     * `URL Schemes` >This creates an array.
     * `Item 0` inside `URL Schemes` > Set it to your custom scheme.
3. **Handle the Deep Link**

Implement the following method in your `AppDelegate.swift` to handle deep links:

```swift
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
    print("openUrl: \(url)")
    return true
}
```

### Universal Links

To handle universal links from Netmera push actions (e.g., `https://your_domain/scheme?query`):

1. **Set Netmera Push Delegate**

In `didFinishLaunchingWithOptions`, add:

```swift
Netmera.setPushDelegate(self)
```

Ensure that your `AppDelegate` conforms to the `NetmeraPushDelegate` protocol.

2. **Implement Universal Link Handlers**

Add the following methods to handle universal links:

* `shouldOpen`: Checks if the URL should be handled. Return `true` to process it.
* `open`: Executes when the URL is triggered by a Netmera push action.

```swift
  /// Returns who will open the given URL in a push context (e.g. link from web view content).
  /// Return `.appHandles` only if you implement `openURL(_:for:)`; otherwise return `.sdkHandles`.
  func urlOpeningDecision(for url: URL, push: NetmeraBasePush) -> PushDelegateDecision {
    if url.host == "your_domain" || UserDefaults.standard.bool(forKey: NotificationDelegateSetting.deeplinkHandling.rawValue) {
      return .appHandles
    }
    return .sdkHandles
  }

  /// Called when the delegate has indicated it will open the URL. Open the URL in your preferred way (e.g. in-app browser or deeplink).
  func openURL(_ url: URL, for push: NetmeraBasePush) {
    print("openURL \(url)")
    showAlertInIndependentWindow(title: "App handling link", message: url.absoluteString)
  }
}
```
