Sub topic 8
Setting up iOS Universal Links
Universal Links let an ordinary https:// URL open your app on iOS instead of Safari. iOS trusts the link only if your domain says your app may handle it. This chapter walks through that two-way handshake and shows how to confirm it works.
This chapter assumes you can build and run a Flutter app on an iOS simulator or device, and that you control a web domain. It also assumes you know the parts of a URL and have read the earlier chapters on link types and on how Flutter receives a link.
How Universal Links work
A Universal Link needs agreement from both sides:
- The app declares which domains it wants to handle, using the Associated Domains entitlement.
- The domain publishes a file,
apple-app-site-association(AASA), that lists which app IDs may open which paths.
When the app is installed, iOS reads the entitlement, downloads the AASA file for each listed domain, and caches the result. If the two sides match, tapping a matching link opens the app. If they do not match, the link opens in Safari. This is the main difference from a custom scheme such as myapp://, which any app can claim.
Step 1: Publish the AASA file
Serve the file at this exact URL:
https://example.com/.well-known/apple-app-site-associationUse this content:
{
"applinks": {
"details": [
{
"appIDs": ["TEAMID.com.example.app"],
"components": [{ "/": "/products/*" }]
}
]
}
}The fields mean the following:
appIDsis your Apple Developer Team ID, a dot, then the bundle identifier. You find the Team ID in your developer account under Membership details.componentslists the URL patterns the app may open. Here, any path that starts with/products/qualifies. The*matches any remaining characters.
The hosting rules are strict, and most setup failures come from breaking one of them:
| Requirement | Detail |
|---|---|
| Protocol | HTTPS with a valid certificate. Self-signed certificates fail. |
| Redirects | The URL must return the file directly. A redirect makes verification fail. |
| File name | `apple-app-site-association`, with no `.json` extension. |
| Content type | `application/json` is recommended. |
| Location | `/.well-known/` on the exact host that appears in the link. |
| Authentication | None. The file must be public. |
You can also restrict or exclude paths. Components are evaluated in order, and the first match wins. This example excludes one path and then allows the rest of /products/:
{
"applinks": {
"details": [
{
"appIDs": ["TEAMID.com.example.app"],
"components": [
{ "/": "/products/internal/*", "exclude": true },
{ "/": "/products/*" }
]
}
]
}
}Put exclusions before the broad rule they carve out of. If you reversed the order, the broad rule would match first and the exclusion would never apply.
Step 2: Add the Associated Domains entitlement
An entitlement is a permission recorded in your signed app. To add this one:
- Open
ios/Runner.xcworkspacein Xcode. Open the.xcworkspacefile, not the.xcodeprojfile. - Select the Runner target, then the Signing & Capabilities tab.
- Click + Capability and add Associated Domains.
- Under Domains, click + and enter
applinks:example.com.
Xcode writes the entry to ios/Runner/Runner.entitlements. You can check the result:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.developer.associated-domains</key>
<array>
<string>applinks:example.com</string>
</array>
</dict>
</plist>Write only the host after applinks:. Do not include https:// or a path. Adding the capability also requires that the app's bundle ID is registered in your developer account with Associated Domains enabled. Xcode usually does this when automatic signing is on. Simulator builds can work without a paid account in some cases, but real devices need a valid signing team.
Step 3: Let Flutter handle the link
On current Flutter versions, deep linking is enabled by default and Flutter forwards incoming URLs to the framework. If you use go_router or another Router-based setup, the URL becomes a route with no extra native code.
Flutter's default handling can be turned off. In ios/Runner/Info.plist, check that you have not set this key to false:
<key>FlutterDeepLinkingEnabled</key>
<true/>You would set it to false only if you handle links yourself with a package such as app_links. Setting it to false while also expecting go_router to receive links is a common source of confusion.
On the Dart side, the router from the earlier chapters is enough:
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
final router = GoRouter(
routes: [
GoRoute(
path: '/',
builder: (context, state) => const HomeScreen(),
),
GoRoute(
path: '/products/:id',
builder: (context, state) {
final id = state.pathParameters['id']!;
final ref = state.uri.queryParameters['ref'];
return ProductScreen(id: id, ref: ref);
},
),
],
errorBuilder: (context, state) => NotFoundScreen(uri: state.uri),
);The AASA file decides whether iOS opens the app. The router decides which screen appears. Keep the two lists of paths in sync, because a path in the AASA file with no matching route lands on your fallback screen.
Step 4: Verify the setup
Check the server side first, then the device.
Check the file
From a terminal, confirm the file is reachable without redirects:
curl -i https://example.com/.well-known/apple-app-site-associationLook for HTTP/2 200 and a JSON body. A 301 or 302 status means a redirect is in the way.
You can also confirm what Apple's CDN sees, since iOS reads the file through it rather than directly from your server:
curl -i "https://app-site-association.cdn-apple.com/a/v1/example.com"The CDN can take some time to refresh after you change the file. If your edit does not appear, the cache is the likely cause.
Test on a simulator or device
Use simctl to open a link in the simulator:
xcrun simctl openurl booted "https://example.com/products/42"On a physical device, paste the link into Notes or Messages and tap it. Typing a URL into Safari's address bar does not trigger Universal Links, because iOS treats that as a request to browse the site. Tapping a link from another app does.
Common failures
| Symptom | Likely cause |
|---|---|
| Link opens Safari | AASA unreachable, wrong Team ID or bundle ID, or the path is not in `components`. |
| Worked once, then stopped | iOS remembers a user's choice. Long-pressing a link and choosing to open in Safari can make iOS keep opening that domain in Safari. |
| Entitlement missing at runtime | Capability added to the wrong target, or the app was not reinstalled after the change. |
| Link opens the app on the home screen only | Flutter's deep linking is disabled, or the router has no matching route. |
| Changes to the AASA file ignored | CDN cache. Wait, or use developer mode on a device. |
Reinstalling the app is often the fastest reset. iOS fetches the AASA file at install time and refreshes it periodically, so a changed file may not apply to an installed app right away.
Limits to remember
- Universal Links work only for
httpslinks. Custom schemes need no verification but can be claimed by other apps. - A link typed or pasted into Safari's address bar opens the website. The behavior applies to taps from other apps and from web pages on a different domain.
- If a user taps a link on the same domain as the page they are viewing, iOS may keep them in the browser. Links from a different domain, or from a banner, open the app.
- The AASA file is public. Do not put secrets in it, and do not treat verification as authentication of the user. Validate incoming paths and parameters in the app, as the later security chapter describes.
Exercise
Your Universal Link opens Safari instead of the app. curl -i https://example.com/.well-known/apple-app-site-association returns HTTP/2 301 with a location header pointing at https://www.example.com/.well-known/apple-app-site-association. What is wrong, and how do you fix it?
Show answer
iOS requires the AASA file to be returned directly, with no redirect, on the exact host used in the link. Serve the file with a 200 response at https://example.com/.well-known/apple-app-site-association, and if you also use www.example.com, serve it there too and add applinks:www.example.com to the Associated Domains list. Then reinstall the app so iOS fetches the file again.
Exercise
An AASA file has the components { "/": "/products/*" } followed by { "/": "/products/internal/*", "exclude": true }. Why does /products/internal/7 still open the app, and how do you correct it?
Show answer
Components are evaluated in order and the first match wins. The broad /products/* rule matches first, so the exclusion is never reached. Move the exclusion rule above the broad rule.