This question already has answers here:
Show screen on first launch only in iOS
(8 answers)
Closed 5 years ago.
I'm trying to create an tutorial screen that displays only during the first opening of an app. i know i should use user defaults but how and where ? in method viewDidLoad or on app delegate class ?
Try this one
- (BOOL)isAppAlreadyLaunchedOnce {
BOOL isRememberMe = [[NSUserDefaults standardUserDefaults]boolForKey:#"isAppAlreadyLaunchedOnce"];
if (isRememberMe) {
return YES;
}
else{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"isAppAlreadyLaunchedOnce"];
return NO;
}
}
Store the status whether the Tutorial View is displayed or not in NSUserDefaults.
If you want to decide at startup to show or not show the Tutorial View you should do it in App Delegate didFinishLaunchingWithOptions
if(![[NSUserDefaults standardUserDefaults] boolForKey:#"shouldShowTutorial"])
//show tutorial
}
else{
//don't show tutorial
}
On the first time when the Tutorial View has been displayed set a flag in NSUserDefaults.
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"shouldShowTutorial"];
#Maddy has given correct answer but let me just clarify one thing why we will not use viewDidLoad?
Why we are using didFinishLaunch
in short:
viewDidLoad
System has already decided which View to load when application launches .By default it's the "Main" storyboard's initial view controller
didFinishLaunch
System has done all tasks to show your app but has not decided which view to load. Thats why you can control here to show your tutorial screen first. :)
Related
Can someone help with a method of how to launch into a view that an might have quit in or suspended in. E.g.
I launch the app.
Go Open a couple of views within the a and end up in View 4.
Quite the app or go into background mode.
I have read that NSUserDefaults can be used but I am just not sure how.
You can save the view in the userdefaults with:
NSString *valueToSave = #"view4";
[[NSUserDefaults standardUserDefaults] setObject:valueToSave forKey:#"lastView"];
[[NSUserDefaults standardUserDefaults] synchronize];
And then you can load the last view from the userdefaults when the user starts your app again (e.g. in the ViewDidLoad in your startView).
NSString *savedValue = [[NSUserDefaults standardUserDefaults]
stringForKey:#"lastView"];
After that you navigate to the view depending on the savedValue.
Keep in mind that you don't really save a view or something. You just save a short string or value that allows you to know which view you want to load again.
Update in Swift:
To save the last view:
let valueToSave = "view4"
let userDefaults = NSUserDefaults.standardUserDefaults()
userDefaults.setValue(valueToSave, forKey: "lastView")
userDefaults.synchronize()
And after the restart of the app:
if let loadedView = userDefaults.valueForKey("lastView") {
// load your view
}
else {
// no last view saved
}
I want to make a guided tour of my app on the first launch, but not a separated guided tour where I show images of the app. I'd like to have multiple chat-bubbles just like this one:
And when I click somewhere on the screen, the next one pops up. I haven't found a single tutorial for this on the web, so maybe I'm just not using the right keywords...
Anyways, thanks a lot !
You can use an Int e.g. int firstLaunch and save it with NSUserDefaults to detect first launch. Then add a view to the UIView in the story board and make it fill the Application screen and give it a grey color and and transparency.
Then add the images of the bubble in the position you want them in the view.
Below would go in the ViewDidLoad:
firstLaunch = [[NSUserDefaults standardUserDefaults] integerForKey:#"FirstLaunch"];
if (firstLaunch == 0) {
firstLaunch = 1;
[[NSUserDefaults standardUserDefaults] setInteger:firstLaunch forKey:#"FirstLaunch"];
ViewName.hidden = NO;
bubble1Image.hidden = NO;
}
My UISegmentedControl will not stay selected. I have made sure that momentary is NO. So the solutions I have come across on here have not helped.
Would someone please be able to point me in the right direction?
EDIT
Thought I might make this question a bit clearer.
I have a UISegmentedControl and it has four selections (10,20,30,40) which changes the amount of questions asked on my quiz page. Making a selection works fine and changes the amount of questions.
But when I leave that view and go back later on to change the amount of questions again, it shows the selected as 10 even if I have selected something else.
How can I keep it showing the actual selected value?
EDIT
The number of questions is saved in NSUserDefaults.
[[NSUserDefaults standardUserDefaults] setInteger:amountOfQuestions forKey:#"Amount"]
How do I initialize a segmented control with a value from NSUserDefaults?
EDIT - Solved
#SettingsViewController .m file.
- (void)viewDidLoad
{
amountOfQuestions = [[NSUserDefaults standardUserDefaults] integerForKey:#"Amount"];
if (amountOfQuestions == 10) {
mySegment.selectedSegmentIndex = 0;
}
I did not have the below code in my IBAction for my segmented control. So when i tried the above code it did not work. Now it works a treat.
[[NSUserDefaults standardUserDefaults]synchronize];
#SettingsViewController .m file.
- (void)viewDidLoad
{
amountOfQuestions = [[NSUserDefaults standardUserDefaults] integerForKey:#"Amount"];
if (amountOfQuestions == 10) {
mySegment.selectedSegmentIndex = 0;
}
I did not have the below code in my IBAction for my segmented control. So when i tried the above code it did not work. Now it works a treat.
[[NSUserDefaults standardUserDefaults]synchronize];
This question already exists:
Load up different view on first run
Closed 9 years ago.
I'm programming an app at the moment which requires on the first run for the app toload a different view in which the user can select its prefered settings.Here's the code
implementation AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
// Determining Storyboard identifier for first view
// Determining Storyboard identifier for first view
NSString *storyboardID = [self hasEverBeenLaunched]? #"MainView" : #"LoginView";
// Setting proper view as a rootViewController
self.window.rootViewController = [self.window.rootViewController.storyboardinstantiateViewControllerWithIdentifier: #"view45"] ;
It then goes on with the following code:
- (BOOL)hasEverBeenLaunched
{
// A boolean which determines if app has eer been launched
BOOL hasBeenLaunched;
// Testig if application has launched before and if it has to show the home-login screen to login
// to social networks (facebook, Twitter)
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"HasAlreadyLaunched"]) {
// Setting variable to YES because app has been launched before
hasBeenLaunched = YES;
NSLog(#"App has been already launched");
} else {
// Setting variable to NO because app hasn't been launched before
hasBeenLaunched = NO;
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"HasAlreadyLaunched"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(#"This is the first run ever...");
}
return hasBeenLaunched;
}
Just a few quick notes: View 45 is the initial starting up view which should only show once, Otherwise the main view controller is ticked under atrributes to be the initial view controller(The one that always loads up at the start, after the first run)
So the problem is that it only ever loads view 45 ,the first run view, but whats causing that exactly ?
The problem is in the following line:
self.window.rootViewController = [self.window.rootViewController.storyboardinstantiateViewControllerWithIdentifier: #"view45"] ;
It is ignoring the value of storyboardID and just using the string #"view45", so that's why you'll always get the same one every single time.
If you want to fix it, change it to the following line:
self.window.rootViewController = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:storyboardID];
You can see that now it is making use of the value in storyboardID.
I want to show a help overlay when a user runs my application for the first time.
To do this, I'm using the following code indidFinishLaunching:
if(![[[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allKeys] containsObject:#"IPHONEFIRSTRUN"])
[[NSUserDefaults standardUserDefaults]setBool:TRUE forKey:#"IPHONEFIRSTRUN"];
In the view controller, I have:
if ([[NSUserDefaults standardUserDefaults]boolForKey:#"IPHONEFIRSTRUN"]==TRUE) {
[self HelpOverlayIphone];
[[NSUserDefaults standardUserDefaults]setBool:FALSE forKey:#"IPHONEFIRSTRUN"];
}
However, it shows the overlay on the second use as well. How can I fix this? Any help is appreciated.
Your logic is overly complex. You are setting permanently a user default to indicate something happening one time. Instead, in the view controller see if the value is not set, if it is do your action and set the variable so that the code is NOT run again:
if ( ! [[NSUserDefaults standardUserDefaults]boolForKey:#"IPHONEFIRSTRUNCOMPLETE"] ) {
[self HelpOverlayIphone];
[[NSUserDefaults standardUserDefaults]setBool:YES forKey:#"IPHONEFIRSTRUNCOMPLETE"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
Take out all the code in the app delegate.
Also it's very likely your original code is not working because you are stopping the app from XCode. If you don't use synchronize user default changes will not be saved in that case (normal quitting of the app does eventually save the changes permanently).
Have you registered the defaults you are using at the start of the program? I would suggest rereading the apple documentation for NSUserDefaults here
You need to first make a call to
- (void)registerDefaults:(NSDictionary *)dictionary
which will only set the key if it doesn't already exist. Then check the key for falseness on the the first run and set it at this point.
My guess is the reason your code isn't working is because the dictionary is never actually being saved in any sort of persistent way.
I have this function which fires when the app fires up- it's always been reliable. I have a uniqueNameOfApp which is just some random hash so that it doesn't collide with other apps.
-(void)loadSettings {
NSMutableDictionary *sttngs = [[NSUserDefaults standardUserDefaults]
objectForKey:uniqueNameOfApp];
if(sttngs != nil) {
[userSettings addEntriesFromDictionary:sttngs];
} else {
_appFiredForFirstTime = YES;
}
}