How to make a screen show only on initial launch

2019-03-04 05:14发布

问题:

I'm trying to make the intro screens for my app - namely the terms of service screen that all users have to agree to the 1st time they download and run the app. obviously, once the users agree, i dont want to show them it again every time they log on the app. what is the best way to go about doing this? I've been reading about NSUserDefaults, but am stuck pretty much after that.

回答1:

In your AppDelegate or whichever class needs to check for first-run status:

Bool isFirstRun = !NSUserDefaults.standardUserDefaults().boolForKey("kAppPreviousLaunchKey")
NSUserDefaults.standardUserDefaults().setBool(true, forKey:"kAppPreviousLaunchKey")
if isFirstRun {
    // React here
}


回答2:

Play with this piece of code in the AppDelegate Class

let launchedBefore = NSUserDefaults.standardUserDefaults().boolForKey("launchedBefore")
    if launchedBefore  {
        print("Not first launch.")
    }
    else {
        print("First launch, setting NSUserDefault.")
        NSUserDefaults.standardUserDefaults().setBool(true, forKey: "launchedBefore")
    }