HomeiOS Developmentios - How you can exit a webframe in Swift primarily based...

ios – How you can exit a webframe in Swift primarily based on a change in a firestore doc


I’m constructing an app in Xcode and one of many screens shows a url in an online body (newUserIntakeView). I have to navigate away from this display screen to the homepageView when the fbIntakeConsultRequired variable adjustments from true to false.

The fbIntakeConsultRequired variable is from my firestore database and adjustments from true to false primarily based on a firebase operate that signifies when the person has accomplished the consumption type on the web site inside the internet body. Whereas I declare the variable in Xcode, I’m unsure methods to preserve the Xcode variable in sync with the firestore doc subject it’s primarily based on.

I’ve tried a number of issues, together with utilizing a didSet variable alongside .onchange and .background modifiers, however it solely navigates to the homepage after the app is closed and reopened, versus navigating to the homepageView the moment the fbIntakeConsultRequired variable adjustments.

Right here’s the code that illustrates this try:


struct newUserIntakeView: View {

import SwiftUI
import AVKit
import GoogleSignInSwift
import GoogleSignIn
import Basis
import Firebase
import FirebaseFirestore

    @State personal var dynamicURL: String?
    @State personal var goToHomepage = false
    @State personal var fbIntakeConsultRequired = false {
        didSet {
            if !fbIntakeConsultRequired {
                goToHomepage = true
            }
        }
    }

extension View {
    func getRootViewController() -> UIViewController {
        guard let display screen = UIApplication.shared.connectedScenes.first as? UIWindowScene else {
            return .init()
        }

        guard let root = display screen.home windows.first?.rootViewController else {
            return .init()
        }
        

        return root
    }
}

    var physique: some View {

                VStack {
                    UcheNavBar(peak: 40, alignment: .main)
                    
                    // Use dynamicURL if obtainable, in any other case use the default URL
                    WebView(url: URL(string: dynamicURL ?? "https://landbot.professional/v3/H-2070328-XTI33DRGBYA0JNST/index.html")!)
                }
                .navigationBarHidden(true)
                .onAppear {
                    // Name personalizeURL operate when the view seems
                    personalizeURL()
                    checkFirebaseIntakeField()
                }
                // name new operate to replace fbI variarble, however verify if there's a approach to make use of a nvagiation hyperlink with a type that's not inside the app
                .onChange(of: fbIntakeConsultRequired) { newValue in
                    if !fbIntakeConsultRequired { // Test the earlier worth
                        // Deal with the case when intakeConsultRequired adjustments from true to false
                        goToHomepage = true
                        print("goToHomepage worth is", goToHomepage)
                        print("fbIntakeConsultRequired worth after change set off is", fbIntakeConsultRequired)
                    }
                }
                .background(
                    NavigationLink (
                        vacation spot: HomePageView(),
                        isActive: $goToHomepage,
                        label: EmptyView.init
                            )
                                .hidden()
                            )
                        }
                        
                        
    
    func personalizeURL() {
        
        guard let person = Auth.auth().currentUser else { return }
        
        let db = Firestore.firestore()
        let usersCollection = db.assortment("customers")
        
        usersCollection.doc(person.uid).getDocument { doc, error in
            if let doc = doc, doc.exists {
                
                let userid = person.uid
                let nameForURL = doc.information()?["firstName"] as? String ?? ""
                
                let rootIntakeURL = "https://landbot.professional/v3/H-2070328-XTI33DRGBYA0JNST/index.html"
                let newURL = rootIntakeURL + "?userid=" + userid + "&identify=" + nameForURL
                
                let fbIntakeConsultRequired = doc.information()?["intakeConsultRequired"] as? Bool ?? false
                print("fbIntakeConsultRequired worth earlier than change set off is", fbIntakeConsultRequired)
                
                // Set the dynamic URL
                dynamicURL = newURL
                print("Dynamic URL is: (newURL)")
                //print("intakeConsultRequired worth earlier than change set off is", intakeConsultRequired)
                print("fbIntakeConsultRequired worth earlier than change set off is", fbIntakeConsultRequired)
                
            } else {
                
                print("Person doesn't exist inside newUserIntakeView")
                
            }
            
        }
        
    }
    
    func checkFirebaseIntakeField () {
        
        guard let person = Auth.auth().currentUser else { return }
        
        let db = Firestore.firestore()
        let usersCollection = db.assortment("customers")
        
        usersCollection.doc(person.uid).getDocument { doc, error in
            if let doc = doc, doc.exists {
                
                let fbIntakeConsultRequired = doc.information()?["intakeConsultRequired"] as? Bool ?? false
                
                
            } else {
                
                print("Person doesn't exist inside checkFirebaseIntakeField")
                
            }
            
        }
    }
}

And right here is the foundation app file that triggers the logic that makes the app navigate to the homepage after the app is closed and reopened (closing and reopening the app triggers the UCHEApp struct):


import SwiftUI
import FirebaseCore
import FirebaseAuth
import FirebaseFirestore
import GoogleSignIn

class AppDelegate: NSObject, UIApplicationDelegate {
  func software(_ software: UIApplication,
                   didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        FirebaseApp.configure()

        guard let clientID = FirebaseApp.app()?.choices.clientID else { return false}
        return true
    }
    
    func software(_ app: UIApplication,
                    open url: URL,
                    choices: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
        return GIDSignIn.sharedInstance.deal with(url)
    }
}

@primary
struct UCHEApp: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) var delegate
    @AppStorage("signIn") var isSignIn = false
    
    init() {
        UITableView.look().tintColor = UIColor.white
    }
    
    var physique: some Scene {
        WindowGroup {
            NavigationStack {
                if !isSignIn {
                    LoginSignUpView()
                } else {
                    // Deal with asynchronous Firestore operation extra elegantly
                    UserContentView()
                }
            }
            .accentColor(.white)
        }
    }
}

struct UserContentView: View {
    @State personal var fbIntakeConsultRequired = false
    
    var physique: some View {
        Group {
            if fbIntakeConsultRequired {
                newUserIntakeView()
            } else {
                HomePageView()
            }
        }
        .onAppear {
            // Fetch person information from Firestore
            fetchUserData()
        }
    }
    
    personal func fetchUserData() {
        guard let person = Auth.auth().currentUser else {
            print("No person is signed in")
            return
        }
        
        let db = Firestore.firestore()
        let usersCollection = db.assortment("customers")
        
        usersCollection.doc(person.uid).getDocument { doc, error in
            if let doc = doc, doc.exists {
                fbIntakeConsultRequired = doc.information()?["intakeConsultRequired"] as? Bool ?? false
                print("fbIntakeConsultRequired worth earlier than change set off is", fbIntakeConsultRequired)
            } else {
                print("Person doesn't exist inside root app operate")
            }
        }
    }
}

I’m conscious there’s a strategy to obtain my desired performance that entails continually listening for adjustments within the fbIntakeConsultRequired subject, however I even have heard that it isn’t scalable.

I’m additionally conscious I might simply add a button to the Navbar on the high, however that may result in a complicated person interface as a result of the fbIntakeConsultRequired variable adjustments from true to false after an consumption type (within the url embedded within the internet body) is accomplished. If there was a button that would navigate away from the shape earlier than it was full, customers would be capable of bypass it and that may result in a poor person expertise.

I’d actually admire ideas for methods to exit an online body view and navigate to a different view in Xcode, utilizing adjustments within the fbIntakeConsultRequired variable because the set off, or another methodology.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments