一旦我们在 SwiftUI iOS 中获得 openurl,如何关闭 SFSafariviewcontroller?
How to close SFSafariviewcontroller once we get openurl in SwiftUI iOS?
我正在开发 SwiftUI 应用程序,我正在导航到 SFSafariviewcontroller(浏览器),一旦登录完成,我将获得 openurl。
我想在获得 openurl 后自动关闭 SFSafariviewcontroller。
感谢您的帮助...以下是我的工作
//how I am calling Safari
.fullScreenCover(isPresented: $isButtonActive, content: {
if let url = loginAction() {
SafariView(url: url)
}
}
//how i am getting openurl
@main
struct MyApplication: App {
var body: some Scene {
WindowGroup {
ContentView()
.onOpenURL { url in
// handle the URL that must be opened
}
}
}
}
我的 safari 代码
import SafariServices
struct Myview: View {
@EnvironmentObject var appConfiguration : AppConfiguration
@State private var isButtonActive = true
@State private var isLoginActive = true
var body: some View {
NavigationView {
Button( buttonTitle: "button" buttonCallback: {
self.isLoginActive = true
}, isActive: $isButtonActive)
}
}
.afklPageView()
.navigationBarTitle("title", displayMode: .inline)
.fullScreenCover(isPresented: $isButtonActive, content: {
if let url = loginAction() {
SafariView(url: url)
}
})
}
}
struct SafariView: UIViewControllerRepresentable {
let url: URL
func makeUIViewController(context: UIViewControllerRepresentableContext<Self>) -> SFSafariViewController {
return SFSafariViewController(url: url)
}
func updateUIViewController(_ uiViewController: SFSafariViewController, context: UIViewControllerRepresentableContext<SafariView>) {
}
}
这是一个基本示例,展示了如何存储 EnvironmentObject
中显示的浏览器状态:
class SafariState : ObservableObject {
@Published var showSafari = false
}
@main struct WhosebugApp: App {
@StateObject var safariState = SafariState()
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(safariState)
.onOpenURL { url in
safariState.showSafari = false
}
}
}
}
struct ContentView: View {
@EnvironmentObject private var safariState : SafariState
var body: some View {
Button("Toggle safari") {
safariState.showSafari.toggle()
//include for testing to show how to dismiss it
//DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
// safariState.showSafari = false
//}
}.fullScreenCover(isPresented: $safariState.showSafari) {
SafariView(url: URL(string: "https://google.com")!)
}
}
}
请注意如何使用 .environmentObject
将 SafariState
传递到视图层次结构中
我正在开发 SwiftUI 应用程序,我正在导航到 SFSafariviewcontroller(浏览器),一旦登录完成,我将获得 openurl。
我想在获得 openurl 后自动关闭 SFSafariviewcontroller。
感谢您的帮助...以下是我的工作
//how I am calling Safari
.fullScreenCover(isPresented: $isButtonActive, content: {
if let url = loginAction() {
SafariView(url: url)
}
}
//how i am getting openurl
@main
struct MyApplication: App {
var body: some Scene {
WindowGroup {
ContentView()
.onOpenURL { url in
// handle the URL that must be opened
}
}
}
}
我的 safari 代码
import SafariServices
struct Myview: View {
@EnvironmentObject var appConfiguration : AppConfiguration
@State private var isButtonActive = true
@State private var isLoginActive = true
var body: some View {
NavigationView {
Button( buttonTitle: "button" buttonCallback: {
self.isLoginActive = true
}, isActive: $isButtonActive)
}
}
.afklPageView()
.navigationBarTitle("title", displayMode: .inline)
.fullScreenCover(isPresented: $isButtonActive, content: {
if let url = loginAction() {
SafariView(url: url)
}
})
}
}
struct SafariView: UIViewControllerRepresentable {
let url: URL
func makeUIViewController(context: UIViewControllerRepresentableContext<Self>) -> SFSafariViewController {
return SFSafariViewController(url: url)
}
func updateUIViewController(_ uiViewController: SFSafariViewController, context: UIViewControllerRepresentableContext<SafariView>) {
}
}
这是一个基本示例,展示了如何存储 EnvironmentObject
中显示的浏览器状态:
class SafariState : ObservableObject {
@Published var showSafari = false
}
@main struct WhosebugApp: App {
@StateObject var safariState = SafariState()
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(safariState)
.onOpenURL { url in
safariState.showSafari = false
}
}
}
}
struct ContentView: View {
@EnvironmentObject private var safariState : SafariState
var body: some View {
Button("Toggle safari") {
safariState.showSafari.toggle()
//include for testing to show how to dismiss it
//DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
// safariState.showSafari = false
//}
}.fullScreenCover(isPresented: $safariState.showSafari) {
SafariView(url: URL(string: "https://google.com")!)
}
}
}
请注意如何使用 .environmentObject
SafariState
传递到视图层次结构中