🧭 SwiftUI 的 SafariServices
在您的应用程序中启用 Web 视图和服务。
使用 SFSafariViewController 作为 openURL 中的一个动作
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.environment(\.openURL, OpenURLAction { url in
.safari(url)
})
}
}
}
您也可以提供一个可选的配置
.environment(\.openURL, OpenURLAction { url in
.safari(url) { configuration in
// Apply your configuration to `configuration`
}
})
在特定的窗口场景中使用 SFSafariViewController 作为 openURL 中的一个动作。
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
WindowSceneReader { windowScene in
ContentView()
.environment(\.openURL, OpenURLAction { url in
.safariWindow(url, in: windowScene)
})
}
}
}
}
在 SFSafariViewController 中显示一个 URL。
struct ContentView: View {
@State private var showURL: URL?
var body: some View {
Button("Show URL) {
showURL = URL(...)
}
.safari(url: $showURL)
}
}
您也可以提供一个可选的配置
.safari(url: $showURL) { configuration in
// Apply your configuration to `configuration`
}
通过以下方式呈现一个身份验证会话
.authenticationSession(isPresented: $isPresented, configuration: AuthenticationSessionConfiguration) { result in
// Handle result
}
或使用预定义的 Button
AuthenticationSessionButton(configuration: AuthenticationSessionConfiguration) {
Text("Authenticate")
} onCompletion: { result in
// Handle result
}
请参阅 LICENSE