Universal Links on iOS provide a seamless way to direct users from a website to specific content within your app. However, it's crucial to understand a key limitation regarding redirects: Universal Links generally do not work with automatic redirects due to security measures implemented by Apple. If a Universal Link is clicked and then automatically redirected, iOS will typically open the web fallback in Safari instead of launching your app.
This means that if your intention is for a redirected link to open your app, it will likely fail, and the user will be taken to the web page in their browser.
Despite this limitation, setting up Universal Links is essential for a good user experience. Here's how to implement them:
1. Enable Associated Domains in Your Xcode Project
First, you need to tell your iOS app that it intends to support Universal Links.
* Open your project in Xcode.
* Select your app's target.
* Go to the "Signing & Capabilities" tab.
* Click the "+ Capability" button and select "Associated Domains."
* Add your domain(s) with the applinks: prefix (e.g., applinks:yourdomain.com).
2. Create and Host the apple-app-site-association (AASA) File
This JSON file tells iOS which URLs on your website should open in your app.
* Create the file: Name it apple-app-site-association (without any file extension).
* Content: The file should contain JSON that specifies your app's bundle ID and the paths it can handle.
```json
{
"applinks": {
"apps": [],
"details": [
{
"appID": "YOUR_TEAM_ID.YOUR_BUNDLE_ID",
"paths": [ "/path/to/content/*", "/another/path/*", "*" ]
}
]
}
}
```
* Replace `YOUR_TEAM_ID` with your Apple Developer Team ID and `YOUR_BUNDLE_ID` with your app's bundle identifier.
* The `paths` array specifies which URLs your app can handle. You can use wildcards (e.g., `*`) to match multiple paths.
- Host the file: Upload this file to the root of your web server or within a
.well-knowndirectory (e.g.,https://yourdomain.com/.well-known/apple-app-site-association).- It must be accessible via HTTPS and without any redirects.
- Ensure the content type is
application/json.
3. Handle Universal Links in Your App
Your app needs to respond when it's launched via a Universal Link.
* Implement the application(_:continue:restorationHandler:) method in your AppDelegate.swift file.
* Inside this method, you can extract the URL and its parameters to navigate the user to the correct content within your app.
```swift
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,
let incomingURL = userActivity.webpageURL else {
return false
}
// Handle the incomingURL, e.g., parse its path and query parameters
print("Incoming URL: \(incomingURL)")
// Example: Navigate to a specific screen based on the URL
// handleUniversalLink(url: incomingURL)
return true
}
```
Considerations for Redirects and Workarounds
- Direct Clicks Only: Universal Links are designed to work when a user directly taps on them.
- Marketing Automation and Tracking: If you use marketing automation tools or require click measurement that involves redirects, users might be sent to the web fallback URL instead of your app. In such cases, consider using a deep linking provider that offers solutions for attribution measurement and integrates directly with email service providers.
- Safari Navigation Arrow: iOS may display a navigation arrow in Safari that, if clicked, can take the user to the associated website even if the app is installed. If that website then redirects to the App Store, it can create a confusing user experience.
- Fallback Mechanism: If your app isn't installed, the Universal Link will open the specified URL in Safari. You can implement a fallback on your website to redirect users to the App Store if the app isn't detected.
Testing Universal Links
- Install your app on a physical iOS device or simulator.
- Email the Universal Link to yourself or host it on a webpage.
- Tap the link from the device's email client or web browser.
- Verify that the link opens your app directly to the intended content. If the app is not installed, check if it redirects to the fallback URL correctly.
- Be aware that it might take some time (up to 24 hours) for Apple's Content Delivery Network (CDN) to fetch your AASA file.