Show screen on first launch only in iOS - objective-c

Tweetbot and Clear show's on the first start of the app a small tutorial screen how the app works. The screen with the small tutorial only pops up on the first start up of the app (1 time)
How and with what can i make a similar thing? Can anyone push me in the right direction?
View i mean:

I'm assuming by Xcode you actually mean iOS.
What you need to do is use the NSUserDefaults class to store a flag indicating whether the user has seen the tutorial screen before.
When your app first loads (or at the point you want to decide whether or not to show the tutorial screen), do something like this:
if(![[NSUserDefaults standardUserDefaults] boolForKey:#"hasSeenTutorial"])
[self displayTutorial];
This checks the saved NSUserDefaults for the current user for a value named "hasSeenTutorial", which won't exist yet. Since it doesn't exist, it will call displayTutorial. displayTutorial refers to your method for creating the tutorial view. You can figure out that part.
Then, once the user closes the tutorial screen:
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"hasSeenTutorial"];
That value will be saved for your user profile, meaning the next time it checks it, it will be true, so displayTutorial won't be called.

In your viewDidLoad:
if (![#"1" isEqualToString:[[NSUserDefaults standardUserDefaults]
objectForKey:#"aValue"]]) {
[[NSUserDefaults standardUserDefaults] setValue:#"1" forKey:#"aValue"];
[[NSUserDefaults standardUserDefaults] synchronize];
//Action here
}

Certainly, if we would like to tell user something about features after update (not only app launched first time), the solution below could be suitable.
In your viewDidLoad:
NSString *currentBundleVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:#"CFBundleVersion"];
NSString *previousBundleVersion = [[NSUserDefaults standardUserDefaults] objectForKey:#"PreviousBundleVersion"];
if (![currentBundleVersion isEqualToString:previousBundleVersion] ) {
// Here you can initialize your introduction view and present it!
}
Once the user closes the intro:
NSString *currentBundleVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:#"CFBundleVersion"];
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
if (standardUserDefaults) {
[standardUserDefaults setObject:currentBundleVersion forKey:#"PreviousBundleVersion"];
[standardUserDefaults synchronize];
}
In this case app bundle version stored in your standardUserDefaults will be differ from current bundle version only after update and shown only once as well as at first launch.

Initialise your user defaults with a BOOL, something called instructionsSeen (or whatever you want) and set it to NO in your App delegate's initialize method.. In your app, test this value and if it is NO display your tutorial screen. As part of showing and displaying this screen, set the instructionsSeen to YES and store it in your defaults.
This way the demo screen will only show on first launch, unless the user uninstalls and installs the app again.
You could also show the demo for a small number of launches (say 3). In this case, don't use BOOL use a number and increment it instead.

Xamarin.iOS Version within AppDelegate:
UIStoryboard storyboard = UIStoryboard.FromName("Main", null);
if (NSUserDefaults.StandardUserDefaults.BoolForKey ("hasSeenTutorial") == false) {
UIViewController vc = storyboard.InstantiateViewController ("StartPageViewController");
this.Window.RootViewController = vc;
} else {
UIViewController vc = storyboard.InstantiateViewController ("NonStartPageViewController");
this.Window.RootViewController = vc;
}
this.Window.MakeKeyAndVisible ();
In my StartPageViewController, I have a button which sets NSUserDefaults to true, so the next time it runs, it will start off with the NonStartPageViewController:
partial void RegisterButton_TouchUpInside (UIButton sender)
{
NSUserDefaults.StandardUserDefaults.SetBool(true,"hasSeenTutorial");
NSUserDefaults.StandardUserDefaults.Synchronize();
}

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (![defaults objectForKey:#"firstRun"]) {
[defaults setObject:[NSDate date] forKey:#"firstRun"];
[self displayTutorial];
}

Swift version :
if !(NSUserDefaults.standardUserDefaults().boolForKey("seenTutorial")) {
//Tutorial part
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "seenTutorial")
}

Everyone is making this more complex and vauge than it needs to be... Simple complete solution.
In the ViewDidLoad:
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"FirstLoadKey"]) {
self.imageView.hidden = YES;
}else{
self.imageView.hidden = NO;
[[NSUserDefaults standardUserDefaults]setBool:YES forKey:#"FirstLoadKey"];
[[NSUserDefaults standardUserDefaults]synchronize];
}

Related

Save the session even if the app is killed Objective C

I'm developing an app in Objective C and everything is cool but i am really new in this, so i was wondering if there is a way to save the session from a user that has already logged in into my app, I save the username + password cause I use them later but when i close the app, (kill it), i have to log in again, so i was hoping that you guys could help me with this:
Here is how i save the session in my code:
NSString *user = [textUser text];
NSString *password = [textPassword text];
[[NSUserDefaults standardUserDefaults] setObject:user forKey:#"userName"];
[[NSUserDefaults standardUserDefaults] setObject:password forKey:#"userPassword"];
[[NSUserDefaults standardUserDefaults] synchronize];//here is where i save the user info but i want it to stay even if the app is killed
So what i think i need is a method that checks if NSUserDefaults is empty or not, then go straight to my home-screen-activity
Thanks!
I'm not sure, whether I understood your Q correctly!? Do you want to know how to check, whether there are already log-in data?
NSString *user = [[NSUserDefaults standardUserDefaults] objectForKey:#"userName"];
if (user)
{
// User name already has been stored
…
}
else
{
// User name is not stored
}
Use this Method to check login:
method returns YES if user already logged in else returns NO.
-(BOOL)CheckLogin{
if ([[NSUserDefaults standardUserDefaults]valueForKey:#"userName"] && [[NSUserDefaults standardUserDefaults]valueForKey:#"userPassword"]) {
//go straight to my home-screen-activity
return YES;
}
else{
//required to login
return NO;
}
}
Hope this helps.

Reload contents at startup

I've been banging my head against the wall for several days trying to understand how to perform an action as soon as the application starts.
Basically I want to download a plist from my website if the user turns on a switch that determines if he wants to download new contents at startup.
Point is that:
"A" class has the method to reload the contents;
"B" class has the switch that, if turned on, tells the delegate to perform the reload contents method as soon as the application starts
Now, I don't know how to tell the AppDelegate to run the method of class "A" if the switch of class "B" is turned on. Obviously I need to use NSUserDefaults, but i'm pretty lost after that.
Can anyone make things clearer? Or, is there a more comfortable workaround to do it?
yes you can do this using NSUserDefaults
in your class b.
-(void)swithChanged
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//check if !null
if(![[defaults objectForKey:#"shouldDownload"]isKindOfClass:[NSNull class]]){
if([(NSNumber*)[defaults objectForKey:#"shouldDownload"]boolValue])
{
[defaults setObject:[NSNumber numberWithInt:0] forKey:#"shouldDownload"];
[defaults synchronize];
}else{
[defaults setObject:[NSNumber numberWithInt:1] forKey:#"shouldDownload"];
[defaults synchronize];
}
}else{
//set your NSUserDefault here for the first time
}
}
in your AppDelegate
- (void)applicationDidBecomeActive:(UIApplication *)application{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//check if !null
if(![[defaults objectForKey:#"shouldDownload"]isKindOfClass:[NSNull class]]){
if([(NSNumber*)[defaults objectForKey:#"shouldDownload"]boolValue])
{
//you can write the downloadData method in this appDelegate,
//[self downloadData]
//OR
AClass *aClass = [AClass alloc]init];
[aClass downloadData];
}else{
//do not download
}
}else{
//the default behaviour of app, download or not?
}
}
Here's a post that could help you understand the flows during application start-up:
http://www.cocoanetics.com/2010/07/understanding-ios-4-backgrounding-and-delegate-messaging
Also, check this post:
applicationWillEnterForeground vs. applicationDidBecomeActive, applicationWillResignActive vs. applicationDidEnterBackground

Trying to save and set UISwitch state with NSUserDefaults

This is supposed to save the value of two UISwitches to NSUserDefaults and then set the switches on load based on the values saved to the defaults. I've been at this for hours but I can't seem to get it to work. Any help would be greatly appreciated.
The toggles are in a View that I'm using for settings. There are only two toggles, so I want to use NSUserDefaults. Toggles default to OFF. I want to toggle to ON and have that change be saved so that I can change the User experience based on the toggle's value. Then, when the User opens the settings again, I want them to display their current saved state.
I think that the value is being saved, but it is not being applied to the Switches when I reenter the Settings View.
- (IBAction)setBeadVibratorBySwitchState:(id)sender
{
if (setBeadVibrator.selected == YES) {
NSUserDefaults *beadSettings = [NSUserDefaults standardUserDefaults];
[beadSettings setBool:NO forKey:#"beadSwitchStatus"];
[beadSettings synchronize];
}
if (setBeadVibrator.selected == NO) {
NSUserDefaults *beadSettings = [NSUserDefaults standardUserDefaults];
[beadSettings setBool:YES forKey:#"beadSwitchStatus"];
[beadSettings synchronize];
}
NSLog(#"Bead Executed");
}
- (IBAction)setDecadeVibratorBySwitchState:(id)sender
{
if (setDecadeVibrator.selected == YES) {
NSUserDefaults *decadeSettings = [NSUserDefaults standardUserDefaults];
[decadeSettings setBool:NO forKey:#"decadeSwitchStatus"];
[decadeSettings synchronize];
}
if (setDecadeVibrator.selected == NO){
NSUserDefaults *decadeSettings = [NSUserDefaults standardUserDefaults];
[decadeSettings setBool:YES forKey:#"decadeSwitchStatus"];
[decadeSettings synchronize];
}
NSLog(#"Decade Executed");
}
- (void)viewDidLoad
{
[super viewDidLoad];
setBeadVibrator.selected = [[NSUserDefaults standardUserDefaults] boolForKey:#"beadSwitchStatus"];
setDecadeVibrator.selected = [[NSUserDefaults standardUserDefaults] boolForKey:#"decadeSwitchStatus"];
}
NSUserDefaults can only store objects of type NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. So, I suggest that you put the state of the switch in an NSString and put that string in the NSUserDefaults. Than when you retrieve the info from the NSUserDefaults, you can use an if statement to set the value of the boolean again.
If you are using UISwitch, you should set the on property, instead of selected.
setBeadVibrator.on = [[NSUserDefaults standardUserDefaults] boolForKey:#"beadSwitchStatus"];
setDecadeVibrator.on = [[NSUserDefaults standardUserDefaults] boolForKey:#"decadeSwitchStatus"];
see UISwitch Class Reference
I would suggest that you put the NSLog statements in the if-statements to see if they are really saved. If not, your UISwitches may not be configured properly with the UIControlEventValueChanged event.
You can read up more from Apple doc. Here is a very detailed website on using UISwitches.

Check for first launch of my application

How would I check if it is the first launch of of my application using NSUserDefaults and running some code for the first time my app opens?
This should point you in the right direction:
static NSString* const hasRunAppOnceKey = #"hasRunAppOnceKey";
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
if ([defaults boolForKey:hasRunAppOnceKey] == NO)
{
// Some code you want to run on first use...
[defaults setBool:YES forKey:hasRunAppOnceKey];
}
The NSUserDefaults answer is the first thing that popped in my head, but upon reflection I will make another suggestion. A bit more work, but it's worth considering. The motive is: sometimes when troubleshooting an app, Apple recommends deleting that app's plist file. It's a fairly ubiquitous troubleshooting technique. I would recommend storing your boolean in your plist file instead of NSUserDefaults.
Disclaimer: I only do iOS development, so I'm not sure how NSUserDefaults and plists interact on the Mac, and I don't know what all is involved in getting your plist to live in ~/Library/Application\ Support/Preferences/com.mycompany.MyAppName.plist
Anyway, I imagine what this requires is having some code which can actually author a "fresh" plist (probably a copy from a template file in your bundle), and you app does this if it launches and does not see a plist. The default plist should not include the flag which lets your users skip the 'first time' code, but if they have opened the app before, and then delete the plist, they should get default behavior back.
This is an important behavior to support where possible, to aide our users if our app ever gives them trouble.
if (![[NSUserDefaults standardUserDefaults] boolForKey:#"hasBeenLaunched"]) {
// Run code on the first launch only ...
[defaults setBool:YES forKey:#"hasBeenLaunched"];
}
You can use NSUserDefaults to save bools, integers, objects into the program and have them available whenever you open it. You can use 'boolForKey' to set a flag called "hasBeenLaunched". By default, this value will be NO when not set. Once you change it to YES, the code in the if condition will never be executed again.
In your main controller class, implement something like this:
static NSString * const MDFirstRunKey = #"MDFirstRun";
#implementation MDAppController
+ (void)initialize {
NSMutableDictionary *defaults = [NSMutableDictionary dictionary];
[defaults setObject:[NSNumber numberWithBool:YES] forKey:MDFirstRunKey];
[[NSUserDefaults standardUserDefaults] registerDefaults:defaults];
// the following if on Mac and is necessary:
[[NSUserDefaultsController sharedUserDefaultsController] setInitialValues:defaults];
}
- (void)applicationDidFinishLaunching:(NSNotification *)notification {
BOOL firstRun = [[[NSUserDefaults standardUserDefaults]
objectForKey:MDFirstRunKey] boolValue];
if (firstRun) {
// do something
[[NSUserDefaults standardUserDefaults] setObject:
[NSNumber numberWithBool:NO] forKey:MDFirstRunKey];
} else {
// do something else
}
}
#end
The +initialize class method is called before an instance of the class it's found in is created; in other words, it is called very early on, and is a good place to set up your default values.
See Preferences and Settings Programming Guide: Registering Your App's Default Preferences for more info.

problem with loading from NSUserDefaults, obj-c

I'm having a problem with NSUserDefaults. I'm saving a name, then trying to access it later in the code from another view controller, but it seems that my key is empty or not saved as when I display the string from the key my label is blank.
-(void) saveName {
NSString *name = nameField.text;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:name forKey:#"playersName"];
[defaults synchronize];
}
-(void) viewDidLoad // method called later on in code from different viewcontroller
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *name = [defaults objectForKey:#"playersName"];
playerName.text = name; // player name == myLabel
if ([[NSUsersDefaults standardUserDefaults] objectForKey:#"playersName"] == nil) {
playerName.text =#" blank";
}
}
My string doesn't seem to be saving to userdefaults as the #"blank" string keeps showing up with my label. I'm not too familiar with NSUserDefaults so any help is much appreciated.
A few things to note when using NSUserDefaults:
calls to syncrhonize method is not necesary and will only make it slower if your program is multi threaded.
Check if the key that you use is being used by other libraries in your project
Check if the value that you set to the key is not nil
Try to use stringForKey: or boolForKey: instead of objectForKey:
I had troubles a few times with NSUserDefaults but in the end it's usually my code that's problematic.
I would suggest you to check whether your '(void) saveName' method is being called or not... Put some breakpoint and see the result