Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I show a view on the first launch only?

Tags:

xcode

ios

I built an application using Xcode 4.5 with storyboards. The first time the app launches I want the initial view controller to appear with terms and conditions that must be accepted to proceed. After that, I want the app to launch and skip over the first view controller and go to the second one.

I know I have to use the NSUserDefaults class and something to the effect of: if ([[NSUserDefaults standard...] boolForKey:@"iHaveAcceptedTheTerms"])

But I have never used this class before and have no idea how to implement this code. Can someone share the specifics of how to do this?

like image 925
jac300 Avatar asked Oct 14 '12 00:10

jac300


People also ask

How do I get the onboarding view on launch?

For this purpose, we use UserDefaults. If there is already a key called “didLaunchBefore” stored, we tell our ViewRouter to show the home view at the app’s launch. When no key can be found, we know that it’s the first time the app launches and we tell our ViewRouter to display the onboarding view.

How do I tell SwiftUI that a certain screen should only launch first?

A lot of people recently asked us how to tell SwiftUI that a certain screen, for instance an onboarding view, should only be shown when the app launches the first time. To implement this functionality, follow the steps below Step 1: Make sure you already set up your onboarding view that should be shown when the app launches for the first time.

How does my viewrouter know when to display the onboarding view?

If there is already a key called “didLaunchBefore” stored, we tell our ViewRouter to show the home view at the app’s launch. When no key can be found, we know that it’s the first time the app launches and we tell our ViewRouter to display the onboarding view.

Where can I watch NASA's Artemis 1 rocket launch?

NASA's huge Artemis 1 rocket is counting down to a second launch attempt to return the moon and when it does, you can watch the historic mission live online for free. The Artemis 1 mission is scheduled to launch on Saturday, Sept. 3, at 2:17 p.m. EDT (1817 GMT) from Pad 39B of NASA's Kennedy Space Center .


2 Answers

In the interests of keeping this question up-to-date, here is a Swift version of the accepted answer.


STEP 1

In your App Delegate, add the following function.

func applicationDidFinishLaunching(application: UIApplication) {
    if !NSUserDefaults.standardUserDefaults().boolForKey("TermsAccepted") {
        NSUserDefaults.standardUserDefaults().setBool(false, forKey: "TermsAccepted")
    }
} 

This will essentially set your TermsAccepted Bool to false if this is the first launch (as Bools are false by default).


STEP 2

In your root view controller (the view controller which loads when your app is launched), you must have a way to see if the terms have been accepted or not and act accordingly.

Add the following function.

override func viewDidAppear(animated: Bool) {
    if NSUserDefaults.standardUserDefaults().boolForKey("TermsAccepted") {
        // Terms have been accepted, proceed as normal
    } else {
        // Terms have not been accepted. Show terms (perhaps using performSegueWithIdentifier)
    }
}

STEP 3

Once the user accepts your conditions, you want to change your TermsAccepted Bool to true. So in the body of the method which handles the acceptance of the terms, add the following line.

NSUserDefaults.standardUserDefaults().setBool(true, forKey: "TermsAccepted")

I hope this helps!

Loic

like image 156
Loic Verrall Avatar answered Oct 08 '22 09:10

Loic Verrall


You put in in your AppDelegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  

//first-time ever defaults check and set
if([[NSUserDefaults standardUserDefaults] boolForKey:@"TermsAccepted"]!=YES)
{
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"TermsAccepted"];
}

Then you implement in your rootViewController the terms and conditions and a way to accept it. You will have to check if the terms are accepted, for example like this:

if ([[NSUserDefaults standardUserDefaults] boolForKey:@"TermsAccepted"]){
    //proceed with app normally
}
else{
//show terms
}

When accepted, the following code will change the default settings:

 if(termsaccepted){
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"TermsAccepted"];
}
like image 38
NightCoder Avatar answered Oct 08 '22 08:10

NightCoder