Set up Universal Links with associated domains and build App Clips.
Universal Links
# Enable: Associated Domains capability
# File: apple-app-site-association (hosted on HTTPS domain)
# Format: JSON with appID and pathsAssociated Domains Setup
// 1. Xcode → Signing & Capabilities → + Capability → Associated Domains
// Add: applinks:yourdomain.com
// Add: applinks:www.yourdomain.com (if needed)// https://yourdomain.com/.well-known/apple-app-site-association
{
"applinks": {
"apps": [],
"details": [
{
"appID": "TEAM_ID.com.example.myapp",
"paths": [
"/products/*",
"/articles/*",
"/profile/*",
"NOT /admin/*"
]
}
]
},
"webcredentials": {
"apps": ["TEAM_ID.com.example.myapp"]
},
"appclips": {
"apps": ["TEAM_ID.com.example.myapp.Clip"]
}
}// 2. Handle Universal Links in SceneDelegate
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,
let url = userActivity.webpageURL else { return }
handleUniversalLink(url)
}
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Handle launch from Universal Link
if let userActivity = connectionOptions.userActivities.first {
if let url = userActivity.webpageURL {
handleUniversalLink(url)
}
}
}
private func handleUniversalLink(_ url: URL) {
print("Universal Link: \(url)")
// Parse and navigate
let path = url.path
if path.starts(with: "/products/") {
let productId = path.replacingOccurrences(of: "/products/", with: "")
navigateToProduct(productId)
} else if path.starts(with: "/articles/") {
let articleId = path.replacingOccurrences(of: "/articles/", with: "")
navigateToArticle(articleId)
}
}
}SwiftUI Deep Linking
// Handle in App
@main
struct MyApp: App {
@State private var deepLink: URL?
var body: some Scene {
WindowGroup {
ContentView()
.onOpenURL { url in
handleDeepLink(url)
}
.onContinueUserActivity(NSUserActivityTypeBrowsingWeb) { userActivity in
if let url = userActivity.webpageURL {
handleDeepLink(url)
}
}
}
}
func handleDeepLink(_ url: URL) {
// Parse URL and navigate
print("Deep link: \(url)")
// Using NavigationStack path
// NavigationRouter.shared.handle(url)
}
}
// Navigation Router
class NavigationRouter: ObservableObject {
static let shared = NavigationRouter()
@Published var path = NavigationPath()
func handle(_ url: URL) {
let components = url.pathComponents.filter { $0 != "/" }
if components.count >= 2 {
switch components[0] {
case "products":
path.append(ProductRoute(id: components[1]))
case "articles":
path.append(ArticleRoute(id: components[1]))
default:
break
}
}
}
}
struct ProductRoute: Hashable { let id: String }
struct ArticleRoute: Hashable { let id: String }
// In ContentView
struct ContentView: View {
@StateObject private var router = NavigationRouter.shared
var body: some View {
NavigationStack(path: $router.path) {
HomeView()
.navigationDestination(for: ProductRoute.self) { route in
ProductDetailView(productId: route.id)
}
.navigationDestination(for: ArticleRoute.self) { route in
ArticleDetailView(articleId: route.id)
}
}
}
}Testing Universal Links
# 1. Host apple-app-site-association on HTTPS domain
# 2. Test with: https://branch.io/resources/universal-links-validator/
# 3. Or: curl -v https://yourdomain.com/.well-known/apple-app-site-association
# 4. Test on device:
# - Notes app: type link, tap
# - Safari: open link
# - Messages: send link to self
# 5. Xcode Console: Look for "continue userActivity" logs// Debug: Check associated domains
func checkAssociatedDomains() {
let domains = Bundle.main.object(forInfoDictionaryKey: "com.apple.developer.associated-domains") as? [String]
print("Associated Domains: \(domains ?? [])")
}App Clips
# 1. File → New → Target → App Clip
# 2. Bundle ID: com.example.myapp.Clip
# 3. Enable: Associated Domains → appclips:yourdomain.com
# 4. Configure invocation URL: https://yourdomain.com/clip?param=value// App Clip Code / NFC / QR / Safari Banner / Links
// Metadata in Info.plist:
<key>NSAppClip</key>
<dict>
<key>NSAppClipRequestEphemeralUserNotification</key>
<true/>
<key>NSAppClipURL</key>
<string>https://yourdomain.com/clip</string>
</dict>// App Clip View
import SwiftUI
@main
struct MyAppClip: App {
var body: some Scene {
WindowGroup {
ClipView()
.onContinueUserActivity(NSUserActivityTypeBrowsingWeb) { userActivity in
if let url = userActivity.webpageURL {
handleClipInvocation(url)
}
}
}
}
func handleClipInvocation(_ url: URL) {
// Extract parameters
let components = URLComponents(url: url, resolvingAgainstBaseURL: false)
if let queryItems = components?.queryItems {
for item in queryItems {
print("\(item.name): \(item.value ?? "")")
}
}
// Navigate to specific content
}
}
struct ClipView: View {
@State private var showFullApp = false
var body: some View {
NavigationStack {
VStack(spacing: 20) {
Image(systemName: "appclip")
.font(.system(size: 60))
.foregroundColor(.blue)
Text("Welcome to App Clip")
.font(.title)
Text("Try the core feature instantly")
.foregroundStyle(.secondary)
Button("Continue") {
// Main action
}
.buttonStyle(.borderedProminent)
.controlSize(.large)
Button("Get Full App") {
showFullApp = true
}
.buttonStyle(.bordered)
}
.padding()
.overlay(alignment: .topTrailing) {
Button("Close") {
// Dismiss
}
.padding()
}
}
.appClipOverlay {
// Show overlay for full app download
}
}
}App Clip to Full App Transition
// Pass data from Clip to full app
struct ClipView: View {
@State private var userData = UserData()
var body: some View {
VStack {
// ... clip content
Button("Continue in Full App") {
// Save data for full app
let encoder = JSONEncoder()
if let data = try? encoder.encode(userData) {
UserDefaults(suiteName: "group.com.example.myapp")?.set(data, forKey: "clipData")
}
// Open full app
UIApplication.shared.open(URL(string: "myapp://fromClip")!)
}
}
}
}
// In full app
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.onOpenURL { url in
if url.scheme == "myapp" && url.host == "fromClip" {
loadClipData()
}
}
}
}
func loadClipData() {
if let data = UserDefaults(suiteName: "group.com.example.myapp")?.data(forKey: "clipData"),
let userData = try? JSONDecoder().decode(UserData.self, from: data) {
// Use clip data
print("Loaded from clip: \(userData)")
}
}
}Handoff & Continuity
// NSUserActivity for Handoff
func updateUserActivity(_ activity: NSUserActivity) {
activity.activityType = "com.example.myapp.viewProduct"
activity.title = "Viewing Product"
activity.isEligibleForHandoff = true
activity.isEligibleForSearch = true
activity.isEligibleForPublicIndexing = true
activity.userInfo = [
"productId": "123",
"category": "electronics"
]
activity.webpageURL = URL(string: "https://yourdomain.com/products/123")
}
// In SwiftUI
struct ProductDetailView: View {
let productId: String
var body: some View {
Text("Product \(productId)")
.userActivity("com.example.myapp.viewProduct") { activity in
activity.title = "Product \(productId)"
activity.userInfo = ["productId": productId]
activity.webpageURL = URL(string: "https://yourdomain.com/products/\(productId)")
}
}
}
// Receive Handoff
func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
if userActivity.activityType == "com.example.myapp.viewProduct",
let productId = userActivity.userInfo?["productId"] as? String {
navigateToProduct(productId)
}
}Spotlight Search
import CoreSpotlight
// Index content for Spotlight
func indexProduct(_ product: Product) {
let attributeSet = CSSearchableItemAttributeSet(contentType: .item)
attributeSet.title = product.name
attributeSet.contentDescription = product.description
attributeSet.thumbnailData = try? Data(contentsOf: product.imageURL)
attributeSet.keywords = product.tags
let item = CSSearchableItem(
uniqueIdentifier: "product_\(product.id)",
domainIdentifier: "products",
attributeSet: attributeSet
)
CSSearchableIndex.default().indexSearchableItems([item]) { error in
if let error = error { print("Index error: \(error)") }
}
}
// Handle Spotlight tap
func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
if userActivity.activityType == CSSearchableItemActionType,
let identifier = userActivity.userInfo?[CSSearchableItemActivityIdentifier] as? String {
let productId = identifier.replacingOccurrences(of: "product_", with: "")
navigateToProduct(productId)
}
}Try It
Add Associated Domains. Host apple-app-site-association. Test Universal Links. Create App Clip target. Test Clip invocation.
# Test AASA: curl -v https://yourdomain.com/.well-known/apple-app-site-association
# Test Universal Link: Open link in Notes/Safari
# Test App Clip: Use App Clip Code or invocation URLUniversal Links & App Clips
- Configure Associated Domains in Xcode
- Host apple-app-site-association on your domain
- Handle Universal Links in SceneDelegate/App
- Create App Clip target
- Build Clip UI with core feature
- Implement Clip → Full App data transfer
- Add Handoff support with NSUserActivity
- Index content for Spotlight search
Universal Links: Associated Domains + apple-app-site-association (HTTPS). Handle in SceneDelegate/onOpenURL/onContinueUserActivity. App Clips: separate target, lightweight, instant. Use App Groups for Clip→App data transfer. Handoff: NSUserActivity with webpageURL.