Skip to Content

Create Universal Links for iOS Apps

Modify an SAP BTP SDK for iOS app to accept universal links. This allows users to navigate to your app via a URL sent by email or from a website.
You will learn

In this tutorial, you will learn how to create universal links for use in an iOS app created using the SAP BTP SDK Assistant for iOS. With universal links, you can link to content inside your app or website in iOS 9 or later.

qualitureRobin van het HofMarch 31, 2021
Created by
qualiture
June 20, 2018
Contributors
qualiture

Prerequisites

To prepare your app to support universal links, you will learn two different ways to create a new (or modify an existing) app ID. You will then modify the application definition on SAP Mobile Services.

For this tutorial, you can use any iOS app you have previously created with the SAP BTP SDK Assistant for iOS. However, the screenshots and parameters used in this tutorial are taken from the TutorialApp app created in tutorial Create a Sample App.

Universal Links for Developers

  • Step 1

    There are two ways you can setup your app for universal links or any other capability you might want to use in the future. You can use Xcode to create a new app project, could also be generated by the Assistant, and Xcode will create all the necessary definitions for you. The other way is going into the Apple Developer Portal and create a new App Identifier manually and assign the needed capabilities there.

    Open up the TutorialApp or any other app and select the .xcodeproj file. Make sure your app is configured properly and Xcode recognizes your Signing Certificate and development team.

    Xcode

    Click on + Capability to add a new capability to your app. In the upcoming dialog search for Associated Domains and add the capability to your project.

    Xcode

    What Xcode will do know is connect to your Apple Developer profile and register or update your App ID to use the Associated Domains capability.

  • Step 2

    Open your running Mobile Services instance and navigate to the Application Links tab for your app definition under Native/Hybrid - TutorialApp.

    In the Application Links tab click on the pencil right next to the Apple Universal Links section to add the Team ID and the Bundle ID.

    Define application link

    Fill out the upcoming form:

    Field Name Value
    Enabled checked
    Team ID <your_team_id>
    Bundle ID com.example.TutorialApp
    Define application link

    Click on OK.

    The Team ID you should’ve copied from Step 1, if not you can find the id on the Apple Developer Portal.

    Mobile Services will ask you to confirm your changes. Always keep in mind that changes on the application links will take up to 10 seconds to be completed. During that time your users are not be able to connect against Mobile Services.

    Define application link

    Confirm the dialog to see your application links.

    Define application link

    Navigate back to the APIs tab. You should see two QR codes, as well as the generated links:

    Define application link

    Leave the page open, you will need the server URL later.

  • Step 3

    Open your app project in Xcode. In your project’s settings, navigate back to Signing & Capabilities and click the + icon located at the Associated Domains section.

    Define application link

    Enter the following domain:

    Field Name Value
    Domain applinks:<server link from Mobile Services>

    Your link should look something like this - applinks:<YourUser-CFSpace>-com-example-tutorialapp.cfapps.eu10.hana.ondemand.com

    Please note the applinks: prefix. It is important you enter it exactly as stated.

    Define application link
  • Step 4

    If you now build and run the app on your device, you will notice an <app_name>.entitlements file is created, containing the just added capability:

    Define application link

    Continue and deploy the app to your physical device and walk through the onboarding process.

  • Step 5

    Once the app is deployed onto your device, it is now running with the added capability.

    The way the universal link work is by the associated domain.

    You have defined the application link on Mobile Services. This creates an apple-app-site-association file. This file associates your Mobile Services account domain with your iOS app.

    The generated apple-app-site-association file contains a JSON structure with the App ID and URL path which is used as universal link.

    Open a browser to https://<YourUser-CFSpace-App>-com-example-tutorialapp.cfapps.eu10.hana.ondemand.com/.well-known/apple-app-site-association and you should see the contents of the file:

    Build and deploy the app

    The asterisk at the end denotes anything. You can for instance add a product id which could directly open an entity with that product ID.

    It is up to the app developer to do something meaningful with the provided context. In the next step, you will simply detect the app is launched via a universal link, and log the URL context to the console.

    Do you need to be authenticated in order to access the apple-app-site-association file?

  • Step 6

    Open your app’s AppDelegate.swift file, and add the following method right below the closing bracket of the class:

    swift
    Copy
    // Universal Link
    extension AppDelegate {
        func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
    
            logger.info("SAP Mobile Services Universal Link called: ")
    
            if userActivity.activityType == NSUserActivityTypeBrowsingWeb {
                let url = userActivity.webpageURL!
                logger.info(url.lastPathComponent)
                // Here you can do whatever you want with the URL components
            }
            return true
        }
    }
    

    When you click/tap on a universal link, this hook will be called. The userActivity object contains a webpageURL property, which holds the universal link you clicked.

    If you provide some context to the universal link URL, it will then be printed to the console by retrieving the lastPathComponent property of the URL.

    Build and deploy the app to your physical device. After it has started, you can dismiss it to the background.

  • Step 7

    The easiest way of testing the universal link is to send it via email or scan the Apple Launch App Code in Mobile Services.

    Try the universal link

    In this example, you simply provide the context start, but it can be anything more meaningful of course.

    If you now click on the link or scan the QR code, it should directly open your app without going to Safari first. If you look in the Xcode console, you see it logged the start context it received via the NSUserActivity hook.

    Try the universal link
Back to top