Link 链接

SwiftUI 提供了一个专用的 Link 视图,它看起来像一个按钮,但按下时会在 Safari 中打开一个 URL。

它很容易使用,只需给按钮一个标题,加上要显示的目标 URL,如下所示:

// Link 默认是跳出当前应用,在 safari 应用中打开

// 声明方式1:
Link(destination: URL(string: "<https://apple.com>")!, label: {
		Text("Visit Apple")
})

// 声明方式2:
Link("Visit Apple", destination: URL(string: "<https://apple.com>")!)

由于它只是一个文本链接,您可以使用字体、颜色等对其进行自定义:

Link("Visit Apple", destination: URL(string: "<https://www.apple.com>")!)
    .font(.title)
    .foregroundStyle(.red)
    
// 记住,默认情况下链接将使用应用程序的强调色,但您可以使用 tint() 修饰符更改它:
Text("[Visit Apple](<https://apple.com>)").tint(.indigo)

如果您想创建自己的视图而不仅仅是使用文本,您也可以通过闭包去自定义:

Link(destination: URL(string: "<https://www.apple.com>")!) {
    Image(systemName: "link.circle.fill")
        .font(.largeTitle)
}

就算不使用 Link,也可以通过 openURL 环境键,让其它视图也可以在 Safari 中打开 URL,如下所示:

struct ContentView: View {

    @Environment(\\.openURL) var openURL

    var body: some View {
        Button("Visit Apple") {
            openURL(URL(string: "<https://www.apple.com>")!)
        }
    }
}

如何自定义打开链接

当用户点击 SwiftUI Text 或 Link 视图中显示的 URL 时,默认情况下它将在 Safari 中打开。但是,您可以通过替换 openURL 环境键来自定义此行为。您可能希望完全处理该链接,或者可能将其传回系统以在自定义操作完成后打开。

例如,此代码调整 Link 和 Text 视图,以便将所有 URL 发送到要执行操作的 handleURL() 方法:

struct ContentView: View {

    var body: some View {
        VStack {
            Link("Visit Apple", destination: URL(string: "<https://apple.com>")!)
            Text("[Visit Apple](<https://apple.com>)")
        }
        // 在环境中声明自定义 OpenURLAction
        .environment(\\.openURL, OpenURLAction(handler: handleURL))
    }

		// 处理 URL 的方法
    func handleURL(_ url: URL) -> OpenURLAction.Result {
        print("Handle \\(url) somehow")
        return .handled
    }
    
}

当您在环境中声明自定义 OpenURLAction,则需要从其处理程序返回其中一个结果值:


ShareLink 分享链接

ShareLink 视图可以轻松共享应用程序中的任何类型的数据,只要它符合 Transferable 协议即可。

它允许用户从我们的应用程序导出内容以在其他地方共享,例如将图片保存到他们的照片库、使用消息向朋友发送链接等等。我们提供想要共享的内容,iOS 负责显示所有可以处理我们发送的数据的应用程序。

// 基本的使用方法如下
ShareLink(item: link)
ShareLink("Learn Swift here", item: link)
ShareLink(item: link, message: Text("Learn Swift here!"))
ShareLink(item: link) {
    Label("Learn Swift here", systemImage: "swift")
}

参数:item

// 共享 URL:这将创建一个带有“共享”图标的按钮,按下该按钮将弹出 iOS 共享表
// 在模拟器中使用ShareLink,可能会无法工作,但如果使用真实设备就可以共享了
ShareLink(
	item: URL(string: "<https://www.hackingwithswift.com>")!
)

参数:subject & message

// 可以给分享加上主题和信息:subject 和 message 是可选的
ShareLink(
		item: URL(string: "<https://www.hackingwithswift.com>")!, 
		subject: Text("Learn Swift here"), 
		message: Text("Check out the 100 Days of SwiftUI!")
)

参数:视图闭包

// 可以通过提供想要的label来自定义按钮本身:
ShareLink(item: URL(string: "<https://www.hackingwithswift.com>")!) {
    Label("Spread the word about Swift", systemImage: "swift")
}

参数:SharePreview

提供要附加的预览在共享更复杂的内容时非常重要。因为其实可以在此处共享完全自定义的数据,预览有助于让接收方了解其中的内容。即使对于本身预览的数据(例如图像)也需要这样做。

//为了避免使代码重复,可以将图像分配给本地常量,然后使用它:
let example = Image(.example)
ShareLink(
		item: example, 
		preview: SharePreview("预览标题", image: example)
	){
    Label("Click to share", systemImage: "airplane")
	}