Yasser Adel
Yasser Adel

Reputation: 31

pushViewController for once

I have one UIButton which called Get Started in the welcome screen, by clicking on this button it will goes to the PhoneNumberViewController to type and click on the next button. For new users it will require to fill out some personal information in the ProfileViewController before going to the HomeViewController. Now I am struggling how can I pop that profile for once since I do not need registered users to check their information when they logout and re-login later.

Here is my code :

    private func checkUser(userId: String) {
    userService.getUser(Uid: userId) { (tutor) in
        if let user = user,
            !user.name.isEmpty && !user.email.isEmpty {
            Router.route(to: .home)
        } else {
            let profileViewController = UIStoryboard.main.viewController(of: ProfileViewController.self)
            profileViewController.isFromOnboarding = true
            self.navigationController?.pushViewController(profileViewController, animated: true)
        }
    }
}

Upvotes: 0

Views: 55

Answers (2)

clawesome
clawesome

Reputation: 1319

Create a hasPushedProfile flag in the controller which can be used to check if the profile view has already been shown or not. On the first time through the flag will be false and will then be set to be true when the profile is displayed, next time through the profile will not display and you can do something else instead.

import UIKit

class LoginController: UIViewController {

    static var hasPushedProfile = false

    private func checkUser(userId: String) {
        userService.getUser(Uid: userId) { (tutor) in
            if let user = user,
                !user.name.isEmpty && !user.email.isEmpty {
                Router.route(to: .home)
            } else {
                if hasPushedProfile == false {
                    hasPushedProfile = true
                    let profileViewController = UIStoryboard.main.viewController(of: ProfileViewController.self)
                    profileViewController.isFromOnboarding = true
                    self.navigationController?.pushViewController(profileViewController, animated: true)
                } else {
                    // Already pushed profile, do something else...
                }
            }
        }
    }
}

Upvotes: 0

Jawad Ali
Jawad Ali

Reputation: 14417

You can save a value in userDefault

private func checkUser(userId: String) {
let isPresented =  UserDefaults.standard.bool(forKey: "isPresented")
    userService.getUser(Uid: userId) { (tutor) in
        if let user = user,
            !user.name.isEmpty && !user.email.isEmpty {
            Router.route(to: .home)
        } else if !isPresented {
            UserDefaults.standard.set(true, forKey: "isPresented")
            let profileViewController = UIStoryboard.main.viewController(of: ProfileViewController.self)
            profileViewController.isFromOnboarding = true
            self.navigationController?.pushViewController(profileViewController, animated: true)
        }
    }
}

Upvotes: 1

Related Questions