I´m building an app for iphone.
I have two views. The first time the user starts the app, i wanna show the 1st view, he pushes a button and go´s to the 2nd view.
The 2nd time he starts the app, i want it to jump directly to the 2nd view.
Can you guys point me in the right direction?
I would use the NSUserDefaults for this
-(BOOL) shouldSkipFirstView
{
//boolForKey returns NO if that entry does not exist or is not associated with a bool
return [[NSUserDeafults standardUserDefaults] boolForKey:#"shouldSkipFirstView"];
}
-(void) skipFirstViewInFuture
{
[[NSUserDeafults standardUserDefaults] setBool:YES forKey:#"shouldSkipFirstView"];
[[NSUserDeafults standardUserDefaults] synchronize]; //optional line
}
-(UIViewController*) getStartupViewController
{
if([self shouldSkipFirstView])
{
[self skipFirstViewInFuture];
return [[[MySecondViewController alloc] init] autorelease];
}
else
{
return [[[MyFirstViewController alloc] init] autorelease];
}
}
You should look into NSUserDefaults. The concept will be to store a value as a preference the first time the app loads and show the 1st view. Then each time your app opens, check if that preference value is set and if so, display the 2nd view.
Create variable and save it to NSUserDefaults so first time when app is loaded set it to true and show view 1 and set it to false. Second time if it is false show view 2 and set it to true.
Code should be in app did finish launching in app delegate.
You just need some kind of record that the app has been opened. You could for example store an object in NSUserDefaults containing the version of the app, which is set on app did finish launching. You can then check to see if there is an object for that key at all, or if the recorded version is lower than the current version of the app (if you want to, for example, show it every time the version changes).
Related
This question already has answers here:
How to get a "first time open"-view for my app?
(3 answers)
Closed 9 years ago.
Is it possible to detect if it is the first time a user opens an iOS application, using Objective-C?
I would like to show a welcome message when the user opens up the app for the first time, but not show it to them after that.
I'm looking for something like:
BOOL firstTime = [AppDelegate isFirstTimeOpeningApplication]
Look for some value in your app's preferences, or the existence of some file. If you don't find it, your app is running for the first time, so write the expected value to the preferences or create the file so that the next time your app runs you'll know that it's not the first time.
If you store the time and date of the first run instead of just a flag, you can determine how long it's been since the app was used. You might want your app to act like it's the first run if the user hasn't used your app in a very long time.
Note that this technique only works if the user hasn't deleted your app. When an app is deleted, all its data is removed. If you want to know if your app has ever run on that device before, even if it was deleted afterward, you'll need to record information to identify the device elsewhere, such as on your own server.
Use NSUserDefaults
//In applicationDidFinishLaunching:withOptions:
BOOL hasLaunchedBefore = [[NSUserDefaults standardUserDefaults] boolForKey:#"AppHasLaunchedBefore"];
if(!hasLaunchedBefore) {
//First launch
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"AppHasLaunchedBefore"];
}
else {
//Not first launch
}
Hope that helps!
You can save a "I've run before" flag in NSUserDefaults.
- (void)applicationDidFinishLaunching:(UIApplication *)application {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *firsttime = [defaults stringForKey:#"firsttime"];
if (firsttime == nil) {
TheOtherViewController *Other = [[TheOtherViewController alloc] initWithNibName:nil bundle:nil];
Other.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[window addSubview: Other.view];
[defaults setObject:#"lasttime" forKey:#"firsttime"];
} else { [window addSubview:viewController.view];
[window makeKeyAndVisible];
}
}
Use NSUserDefaults to store a flag which will indicate if the user has launched the app before or not. If the value of flag .. lets say "IsFirstTimeLaunched" is false then it means the user has not launched the app before.
I'm writing a game for iOS and I was wondering how to make the instructions View Controller open every time the app is opened. I want to have a switch that says "Show me this every time." and if they switch it to no the instructions will no longer show up when the app is opened.
You can use NSUserDefaults to store the switch value, then check for it every time app launches in Your app delegate, applicationDidBecomeActive method.
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
BOOL switchState = [[NSUserDefaults standardUserDefaults] boolForKey:#"switchKey"];
if(switchState) {
//If switch is on create the instance of InstructionViewController
//you can call any of InstructionViewController methods on it.
InstructionViewController* intructionsViewController = [[InstructionViewController alloc] init];
//Present the instance of instruction view on top of your current view
[self.window.rootViewController presentViewController:controller animated:YES completion:nil];
}
}
My questions is...how to make....this...
I am trying to make a scollview to show the term&condition at beginning of my app when the user is 1st time using the app.
if the user accepted the T&C (by clicking accept button), this T&C scollview will never show up again at beginning of the app, as he already accepted. So he will be free to use the app in future.
How do I implement this? any suggestions?
Use NSUserDefaults with a key like "TCShown". If the key does not exist in the NSUserDefaults at the beginning of the launch, you show the T&C and create "TCShown" value, set it to YES ([NSNumber numberWithBool:YES];) and store it to the NSUserDefaults.
Edit:
Assuming that you want to present the T&C in your first viewController,
#define kTCViewedFlag = #"tcViewed"
-(void) viewDidAppear {
NSUserDefaults *myDefaults = [NSUserDefaults standardUserDefaults];
if(![myDefaults objectForKey:kTCViewedFlag]) {
//show the TC
}
}
-(IBAction) userAcceptedTC {
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:YES] forKey:kTCViewedFlag];
[[NSUserDefaults standardUserDefaults] synchronize];
//dismiss the scrollView
}
-(IBAction) userDidDeclineTC {
//handle refusal of TC
}
In addition to Kaan's answer, you can add the TCShown field to the server and update the values accordingly. This will take care of the case when the user who has already accepted the T&C's logs in from a different device.
Maybe you'll find this useful: RLAgreement View Controller
This project allows developers to include
and Agreement, Terms of Service, Non Disclosure Agreement, etc. to an
iPhone App. The controller stores a variable in the user's settings
when the user has a valid agreement and it checks every time the user
opens the App.
I realise that there are other topics like this, but none of them really help. I'm trying to get variables from one view into another, but I have absolutely no idea how.
To give some backstory, my game is a fruit ninja like game where stuff goes on the screen and you have to slice it. If 3 sprites leave the screen unsliced, the game is over and it flips to the game over view screen with a button to go back. Additionally, this SHOULD go to the "flipSideView", which is the highscore, but my implementation of the flipSideView transition doesn't work. This isn't the main issue, the main issue is that I don't know how to get the score from the game in the mainView (which stores it as an int) into the flipSideView (which has the player name).
The main view changes to the gameOverView through this condition in the tick method (which performs regular checks and methods for the game)
if (lifeCounter < 1)
{
gameIsOver = YES;
[self showInfo:0];
[self viewGameOverScreen];
}
That goes to the gameOverView, which will sit there until the replay button is pressed with:
- (IBAction)replayAction:(id)sender
{
[self.delegate gameOverViewControllerDidFinish: self];
}
gameOverViewControllerDidFinish restarts the game and dismisses the view.
- (void) gameOverViewControllerDidFinish: (GameOverViewController *) controller
{
[self restart];
[self dismissModalViewControllerAnimated: YES];
}
The restart method just resets the 3 primary values in the main view (The score, the level, the lives).
As it restarts, the game should take the score and whatever name is stored in the text field in the flipSideView (which can be viewed at any one time gameplay) and store it somehow for future reference. It's not supposed to store it in a file yet because that's next week's task.
I don't wanna post the entire program due to plagiarism issues, but if there are additional parts that might make it easier to understand, I will definitely post them.
Thanks in advance,
Christian
Use Search
NSUserDefaults *currentScore = [NSUserDefaults standardUserDefaults];
[currentDefaults setFloat:yourScoreVariable forKey:#"HighScore"];
[currentDefaults synchronize];
Above to store the score.
Below to retrieve the score.
CGFloat fltHighScore;
NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
fltHighScore = [currentDefaults floatForKey:#"HighScore"];
Suggestion:
You could put the variable in your Controller class,where they both could access.
It is always better to have sharable data in Controller then views if data has to be shared among views.
Update the shareable data through delegate method.
You can use your app delegate for this. You need to declare your variable there, and then you can access this in the following way throughout your whole app:
TestAppDelegate *appDelegate = (TestAppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *string = [appDelegate.Placemarks objectForKey:appDelegate.title];
Don't know if it's the best way tough...
Luck with it!
I am creating an app which requires the users to enter certain values when the app is used for the first time.
A settings screen with 4 UITextFields and a UIPicker.
This settings view can be accessed later using a button from the mainscreen.
Somebody please suggest some ideas
Thanks
Use the NSUserDefaults and set a BOOL or a NSDate to indicate that you app has been used for the first time.
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"firstUse"]