Gerardo Torres
Gerardo Torres

Reputation: 81

How to show once an UIVIewController in swift

I’m making an app, and I need to show only one time the on boarding view controller, so when the user re enter the app the on boarding view controller doesn’t appear any more.

Upvotes: 0

Views: 260

Answers (1)

Pak Ho Cheung
Pak Ho Cheung

Reputation: 1416

I guess the on boarding view controller is your initial view controller. Try this in app delegate

First put checkFunction() in didFinishLaunchingWithOptions then set the identity (eg. Home) for the view controller you want to go

func checkFunction() {
    let acceptedTerms = UserDefaults.standard.string(forKey: "acceptedTerms")
    if acceptedTerms != nil && acceptedTerms == "Yes" {
        let storyBoard = UIStoryboard(name: "Main", bundle: nil)
        let VC = storyBoard.instantiateViewController(withIdentifier: "Home")
        self.window?.rootViewController = VC
    }
}

Second, modify UserDefaults.standard.string in somewhere

// example
@IBAction func acceptButtonTapped(_ sender: AnyObject) {
    UserDefaults.standard.set("Yes", forKey: "acceptedTerms")
    UserDefaults.standard.synchronize()
}

Upvotes: 1

Related Questions