iOS Deeplink

Deep Linking

When sending a push notification with custom action buttons, you can redirect users to any custom page or view in your app by specifying deep links as custom action button URLs. To achieve this, you first need to create a URL scheme (netmera://) in your project.

Using Xcode, edit your Info.plist file:

  • Add a new key, "URL types." Xcode will automatically make this an array containing a dictionary called "Item 0."

  • Within "Item 0," add a key, "URL identifier," and set the value to your custom scheme.

  • Within "Item 0," add a key, "URL Schemes." This will automatically be an array containing a string called "Item 0."

  • Set "URL Schemes » Item 0" to your custom scheme.

Once you've completed these steps, you can confirm that your new URL scheme has been added to your app's Info.plist file. It should look like this:

func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool
{
    //handle URL here to navigate to custom views

    return true
}

After setting up the URL scheme, you should add the application:openURL:options: method to your app delegate.

if (url.scheme == "netmera")
{
    if (url.host == "screenOne")
    {
        // present view controller for screenOne;
    }
    else if (url.host == "screenTwo")
    {
        // present view controller for screenTwo;
    }

    // or you can use host property directly
}

In this method, you can check the passed URL for custom view navigation using the scheme and host properties. For example, if you've set the custom action button URLs as netmera://screenOne and netmera://screenTwo, you can use something similar to this snippet.

The Netmera SDK will trigger the openURL method when users click on the notifications received by their devices. If your application integrates multiple SDKs, there might be situations where the openURL method is not called due to potential override issues. To address this, you can manage deep linking effectively using the Netmera Delegate and these methods.

//Extends NetmeraPushDelegate

//in didFinishLaunchingWithOptions
Netmera.setPushDelegate(self)
func shouldHandleOpen(_ url: URL!, for object: NetmeraPushObject!) -> Bool {
// Deeplink is checked(valid or invalid)
    return true
  }

  func handleOpen(_ url: URL!, for object: NetmeraPushObject!) {
//Control and redirect method
  print("handleOpenUrl: \(url), for pushObject: \(object)")
}

Configure the Project

  • Add the "Associated Domains" capability to the project.

  • In the domain section, add an entry for each domain that the app supports, prefixed with applinks:, such as applinks:www.mywebsite.com.

Configure the Website

  • Create an apple-app-site-association file with the appID and the paths the app supports (e.g., naturalworld.com/products). The format matches that of a JSON file, but it cannot have the .json extension.

  • The appID consists of the team ID (found in the Apple Developer Account in the membership section) combined with the app’s bundle ID (found in the project).

  • Specify the paths the app will support.

{
    "applinks": {
        "apps": [],
        "details": [{
                "appID": "4321.com.netmera.naturalworld",
                "paths": ["/products"]
            }
        ]
    }
}
  • Host your apple-app-site-association file on your domain. It should be uploaded to your HTTPS web server. You can place the file at the root of your server or in the ".well-known" subdirectory.

Handle URL

To use Universal Links, ensure you have added a registered domain to your app’s capabilities and have uploaded an apple-app-site-association file. Then, implement the method application:continueUserActivity:restorationHandler: in your AppDelegate. For example:

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
  if (userActivity.activityType == NSUserActivityTypeBrowsingWeb) {
    let url = userActivity.webpageURL
    // Handle url
  }
  return true
}

When using the userActivity methods to handle Universal Links, the user will be prompted with the question "Open in Safari or in the application?" when clicking on the notification. However, if you handle Universal Links with Netmera Delegate methods (shouldHandleOpenUrl and handleOpenUrl), the user will be directed straight to the app. You can find more information about Netmera Delegate methods, specifically shouldHandleOpenUrl and handleOpenUrl, in the documentation here.

Last updated