Search code examples
swiftuiviewnavigation

I want to move the view when I log in successfully


    Button {
                
                if(UserApi.isKakaoTalkLoginAvailable()){
                    UserApi.shared.loginWithKakaoTalk { (oauthToken, error) in
                        print(oauthToken)
                        print(error)
                    }
                } else {
                    UserApi.shared.loginWithKakaoAccount {(oauthToken, error) in
                        print(oauthToken)
                        print(error)
                    }
                }
                UserApi.shared.loginWithKakaoAccount(prompts: [.Login]) {(oauthToken, error) in
                    if let error = error {
                        print(error)
                    } else {
                        print("LoginSuccess")
                            MainView()// I want to go to this view 
                        _ = oauthToken
                    }
                }
            }

I tried navigation, but it didn't work

I want to call View when Token is here Token type is Bool


Solution

  • you could try this:

    @State var showMainView = false   // <-- here
    
    if showMainView {  // <-- here
        MainView() // I want to go to this view
    } else {
        Button {
            if(UserApi.isKakaoTalkLoginAvailable()){
                UserApi.shared.loginWithKakaoTalk { (oauthToken, error) in
                    print(oauthToken)
                    print(error)
                }
            } else {
                UserApi.shared.loginWithKakaoAccount {(oauthToken, error) in
                    print(oauthToken)
                    print(error)
                }
            }
            UserApi.shared.loginWithKakaoAccount(prompts: [.Login]) {(oauthToken, error) in
                if let error = error {
                    print(error)
                } else {
                    print("LoginSuccess")
                    self.showMainView = true   // <-- here
                    // or DispatchQueue.main.async { self.showMainView = true }
                    _ = oauthToken
                }
            }
        }
    }
    

    You should also read this: https://developer.apple.com/tutorials/swiftui/

    Note, it would be better to do all these UserApi stuff in a separate dedicated class, rather than inside the Button.

    EDIT-1: you can also use NavigationLink, for example:

    struct ContentView: View {
        @State var showMainView = false
        
        var body: some View {
            NavigationView {
                VStack {
                    Button("Login") { doAPIStuff() }
                    NavigationLink("", destination: MainView(), isActive: $showMainView)
                }
            }
        }
        
        func doAPIStuff() {
            if(UserApi.isKakaoTalkLoginAvailable()){
                UserApi.shared.loginWithKakaoTalk { (oauthToken, error) in
                    print(oauthToken)
                    print(error)
                }
            } else {
                UserApi.shared.loginWithKakaoAccount {(oauthToken, error) in
                    print(oauthToken)
                    print(error)
                }
            }
            UserApi.shared.loginWithKakaoAccount(prompts: [.Login]) {(oauthToken, error) in
                if let error = error {
                    print(error)
                } else {
                    print("LoginSuccess")
                    DispatchQueue.main.async { self.showMainView = true } // <-- here
                    _ = oauthToken
                }
            }
        }
    }