How to Serve iOS and Android App Link Files Correctly

Explains how to correctly host and serve the `apple-app-site-association` file │ for iOS Universal Links and the `assetlinks.json` file for Android App Links.

Intermediate

To correctly serve iOS and Android App Link files, you need to configure both your website and your mobile applications. This involves creating and hosting specific files on your web server and making corresponding configurations within your iOS and Android projects.

iOS Universal Links

For iOS, you need to set up Universal Links, which allow users to tap a link to your website and be seamlessly redirected to your installed app.

  1. Create the apple-app-site-association file:

    • Create a JSON file named apple-app-site-association (without a .json extension).
    • This file declares which URLs your app can handle.
    • The file should contain an applinks dictionary. Inside applinks, include an empty apps array and a details array. Each object in the details array should specify the appID (your Apple Team ID followed by your app's Bundle Identifier) and an array of paths that your app can handle. You can use wildcards (* and ?) for path matching.
    • Example apple-app-site-association content: json { "applinks": { "apps": [], "details": [ { "appID": "YOUR_TEAM_ID.com.yourcompany.yourapp", "paths": [ "/path/to/content/*", "/another/path/*", "NOT /path/to/exclude" ] } ] } }
  2. Host the file on your web server:

    • The apple-app-site-association file must be hosted at the root of your domain or within the .well-known subdirectory (e.g., https://yourdomain.com/apple-app-site-association or https://yourdomain.com/.well-known/apple-app-site-association).
    • It must be served over HTTPS with a valid SSL certificate.
    • The file must be accessible directly without any HTTP redirects (301 or 302).
    • The MIME type for the file should be application/json.
    • The uncompressed file size should not exceed 128 KB.
    • If you support multiple domains or subdomains, each domain needs its own apple-app-site-association file.
  3. Configure your iOS app:

    • In Xcode, enable the "Associated Domains" capability for your project.
    • Add the domains your app will support to the "Associated Domains" list, prefixed with applinks: (e.g., applinks:yourdomain.com).
    • Implement the necessary methods in your app's AppDelegate or SceneDelegate to handle the incoming Universal Link URLs.

Android App Links

For Android, App Links provide a similar seamless experience, allowing verified links to open directly in your app without a disambiguation dialog.

  1. Create the assetlinks.json file:

    • Create a JSON file named assetlinks.json.
    • This file establishes a trusted association between your website and your Android app.
    • The file should contain a JSON array with an object that specifies the relation (e.g., delegate_permission/common.handle_all_urls), target (including namespace as android_app, your app's package_name, and the sha256_cert_fingerprints of your app's signing certificate).
    • You can obtain the SHA256 fingerprint using the keytool command or from the Google Play Console if you use Play App Signing.
    • Example assetlinks.json content: json [{ "relation": ["delegate_permission/common.handle_all_urls"], "target": { "namespace": "android_app", "package_name": "com.yourcompany.yourapp", "sha256_cert_fingerprints": ["AA:BB:CC:DD:EE:FF:11:22:33:44:55:66:77:88:99:00:11:22:33:44:55:66:77:88:99:00:AA:BB:CC:DD:EE:FF"] } }]
  2. Host the file on your web server:

    • The assetlinks.json file must be hosted at https://yourdomain.com/.well-known/assetlinks.json.
    • It must be served over HTTPS with a valid SSL certificate.
    • The file must be accessible directly without any HTTP redirects (301 or 302).
    • The MIME type for the file should be application/json.
    • If your app links support multiple host domains, you must publish the assetlinks.json file on each domain.
  3. Configure your Android app:

    • Add an <intent-filter> to your app's AndroidManifest.xml for the URLs you want to handle.
    • Include android:autoVerify="true" in the <intent-filter>. This tells Android to verify the host domain.
    • The <intent-filter> should include ACTION_VIEW, BROWSABLE, and DEFAULT categories.
    • Specify the <data> scheme (e.g., https), host, and any optional path prefixes or patterns.
    • Example AndroidManifest.xml intent-filter: xml <activity android:name=".MainActivity"> <intent-filter android:autoVerify="true"> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="https" android:host="yourdomain.com" android:pathPrefix="/path/to/content" /> </intent-filter> </activity>
    • Handle the incoming Intent in your app's activity to extract data from the URL and navigate to the appropriate content.

General Best Practices

  • HTTPS is mandatory for both iOS Universal Links and Android App Links.
  • Avoid redirects when serving these association files.
  • Test thoroughly on real devices and use validation tools (e.g., Google's Digital Asset Links API for Android, and various online validators for iOS AASA files) to ensure correct setup.
  • Ensure you own the domains for which you are configuring app links.