iosswiftuisfsafariviewcontroller

How to close SFSafariviewcontroller once we get openurl in SwiftUI iOS?


I am working on SwiftUI app where i am navigating to SFSafariviewcontroller (Browser) and once login complete I am getting openurl.

I want close SFSafariviewcontroller automatically once I get openurl.

Thank You for help...below is my work

//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
            }
        }
      }
    }

enter image description here

my code for 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>) {
        
    }
}

Solution

  • Here's a basic example showing how you could store the state of the browser being shown in an EnvironmentObject:

    class SafariState : ObservableObject {
        @Published var showSafari = false
    }
    
    @main struct StackOverflowApp: 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")!)
            }
        }
    }
    

    Note how SafariState is passed into the view hierarchy using .environmentObject