1

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>) {
        
    }
}
Rahul
  • 537
  • 1
  • 7
  • 19
  • I don't think it's possible to know you should close the `SFSafariViewController` unless we know how you opened it in the first place. – jnpdx Jul 23 '21 at 04:50
  • I updated the question how I am calling URL to open SFSafariciewcontroller...will this help? – Rahul Jul 23 '21 at 04:55
  • A little. You need to set `isButtonActive` to `false`. That probably means having some sort of state that you share all the way from the root level of your app down to whatever view is showing the `fullScreenCover`. Do you know how to use an `environment` or `environmentObject`? Or how to pass state down through your views? – jnpdx Jul 23 '21 at 04:58
  • Thanks for pointer..I need to check how to use them as I for now I am not sure.It will be helpful if you can have some example – Rahul Jul 23 '21 at 05:02
  • @jnpdx i understand concept but doubt is i dont see any way to close safari rather than clicking manually on done button...but i want to close safari automatically once it for openurl...can you help in this? – Rahul Jul 23 '21 at 06:44
  • Show your code for SafariView – jnpdx Jul 23 '21 at 06:53
  • I updated my question..Plz have a look – Rahul Jul 23 '21 at 07:34

1 Answers1

1

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

jnpdx
  • 45,847
  • 6
  • 64
  • 94