在 SwiftUI 中处理深度链接需要配置应用程序以响应特定 URL,然后解析这些 URL 以导航至应用程序内的相应内容。深度链接主要分为两种类型:自定义 URL 方案和通用链接。
1. 自定义 URL 方案
自定义 URL 方案允许您定义一个唯一的前缀(例如 myapp://),点击该前缀即可启动应用程序。
配置步骤:
- 在Xcode中添加URL类型:
- 在Xcode项目中选择目标文件。
- 切换至"信息"选项卡。
- 在"URL类型"区域点击 "+"按钮添加新URL类型。
- 设置"标识符"(例如
com.yourcompany.yourapp)。最佳实践是使用应用程序的包标识符作为前缀。 - 将"URL方案"设置为所需方案(例如
myapp)。
SwiftUI 中的处理方式:
使用.onOpenURL视图修饰符处理传入URL。此修饰符自iOS 14起可用。
import SwiftUI
struct ContentView: View { @State private var receivedURL: URL? var body: some View { VStack {
文本("深度链接处理器")
if let url = 接收URL { 文本("接收到的URL:\(url.absoluteString)")
// 解析URL并执行相应导航
// 示例:myapp://product?id=123 if url.host == "product", let components = URLComponents(url: url, resolvingAgainstBaseURL: false), let id = components.queryItems?.first其中: { $0.名称 == "id" })?.value { 文本("产品ID: \(id)")
} } else {
文本("尚未收到深度链接。") } } .onOpenURL { url in receivedURL = url
print("接收到的自定义URL方案:\(url)") } } } 2. 通用链接
通用链接是标准的HTTPS链接(例如https://yourwebsite.com/path),若设备已安装应用则直接启动,否则跳转至网站。这种方式能提供无缝的用户体验。
配置步骤:
-
关联域名权限:
- 在 Xcode 项目中选择目标文件。
- 进入"签名与功能"选项卡。
- 点击"+ 功能"并添加"关联域名"。
- 按格式
applinks:yourwebsite.com添加条目(例如applinks:example.com)。
-
apple-app-site-association文件:- 创建名为
apple-app-site-association的 JSON 文件(不带.json扩展名)。 - 将该文件托管在您网站的根目录或
.well-known目录下(例如:https://yourwebsite.com/apple-app-site-association或https://yourwebsite.com/.well-known/apple-app-site-association)。 - 该文件用于指定网站上哪些路径应打开您的应用。创建此文件需提供团队 ID 和应用程序包 ID。
示例
apple-app-site-association文件:json { "applinks": { "details": [ { "appIDs": [ "YOUR_TEAM_ID.com.yourcompany.yourapp" ], "components": [ { "/": "/products/*", "comment": "匹配 /products/ 路径下的任意 URL" }, { "/": "/profile/*", "comment": "匹配 /profile/ 路径下的任意 URL" } ] } ] } } - 创建名为
SwiftUI 中的处理方式:
与自定义 URL 方案类似,使用 .onOpenURL 修饰符即可实现。
import SwiftUI
struct ContentView: View { @State private var receivedUniversalLink: URL? var body: some View { VStack {
文本("通用链接处理程序") if let url = receivedUniversalLink { Text("接收到的通用链接: \(url.absoluteString)") // 解析URL并跳转 // 示例:https://yourwebsite.com/products/123
if url.pathComponents.contains("products"), let productId = url.lastPathComponent { 文本("通用链接中的产品ID:\(productId)") } } else { 文本("尚未收到通用链接。") } }
onOpenURL { url in receivedUniversalLink = url print("接收通用链接: \(url)")
} } }
3. 解析与导航
对于更复杂的应用程序,您需要解析传入的URL,并使用SwiftUI的导航工具将用户引导至正确的视图。
import SwiftUI enum AppRoute: Hashable {
case home case product(id: 字符串) 案例 配置文件(id: 字符串)
案例 设置 }
类 DeepLinkManager: ObservableObject {
@Published var navigationPath = NavigationPath() func handleURL(_ url: URL) { guard let host = url.主机 else { 返回 }
// 自定义 URL 方案示例:myapp://product?id=123
// 通用链接示例:https://yourwebsite.com/products/123 if host == "product" || url.pathComponents.contains("products") { if let components = URLComponents(url: url, resolvingAgainstBaseURL: false),
let id = components.查询项?首项(条件: { $0.name == "id" })?.value ?? url.lastPathComponent {
导航路径。追加(应用程序路由.product(id: id))
} } else if host == "profile" || url.pathComponents.包含("profile") { if let components = URLComponents(url: url, resolvingAgainstBaseURL: false), let id = components.queryItems?.first(where: { $0.name == "id" })?.value ?? url.lastPathComponent {
导航路径。追加(应用程序路由.profile(id: id)) }
} // 为其他路径添加更多路由逻辑 } }