Remember Apps last screen in iPhone app - iphone

I want to remember the last screen of my app, means if I reopen my app, app should navigate me to the last screen where I were just before exiting app.
Thanks
Saurabh

Take a look at the NSUserDefaults API.
You could save an object for the current screen in the NSUserDefaults then check for that object on launch.
Create a NSMutableDictionary object when a view loads, and change it's value each time, and store it in the user defaults.
NSMutableDictionary *currentScreen = [[NSMutableDictionary alloc] init];
[currentScreen setObject:#"AboutPage" forKey:#"screen"];
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
[standardUserDefaults setObject:currentScreen forKey:#"lastScreen"];
Then when the app loads, check the user defaults "lastScreen" key and load the appropriate view.
Hope this helps.

in iOS4 it's done automatically, because when you quit your application it's actually enters the background mode and doesn't quit. So when you "launch" it again, it opens at the same place the user left.
Previous versions of iOS don't have this feature and you should take care of it by yourself.

Depending on your application, you may want to think about using the Three20 library. This library has the sort of persistence you are looking for built in.

Related

How to make scollView (for T&C) only show up once at begin of app, then never show up again?

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.

Update UI on settings change - Objective-C

For my application, I am storing a url in a settings panel so the user is able to edit it.
After the application is already open, I want to take care of the use case where the user presses the home button to background the application, goes into the settings panel, changes the url, and then wakes the application back up. The problem I am running into is when the application wakes back up and I try and get the value from the settings again, it is the same. The application only notices the change if the app is fully exited and then restarted.
Currently I am reading the settings like this:
NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
NSString *url = [settings stringForKey:"#url"];
This is wrapped inside of a function that gets invoked when UIApplicationDidBecomeActiveNotification gets fired.
Try to synchronize the defaults after setting the value :
[settings synchronize];

Any way to show a view only once the app is used?

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"]

NSUserDefaults not working on iPhone 4.0 device?

i am using NSUserDefaults to save data from the app and when the user leaves the app and then come back to it all the values are restored.
the thing is that it does save the values while the app is running in background mode. but when i close the app from multitasking and re-open it the app comes back as new with no values saved. i have added the methods beginInBackground and applicationWillterminate
but no help is their a new way of doing this on the new 4.0?
PS. i am testing on the device only. i could not use the simulator because i am using Map kit.
Are you calling the synchronize method?
In your applicationDidEnterBackground just use the following code:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults synchronize];
The problem you are having is that you are expecting an NSApplicationWillTerminateNotification that is not getting sent in 4.0 because NSApplicationDidEnterBackgroundNotification is getting sent instead. I have had the same problem. Register both notifications for your storage function and it will work again. You don't need conditional code since even if you register for a notification that does not exist in 3.0, it will never get called there so there will be no crash.
If you are not using notifications you can also override applicationDidEnterBackground: as noted in the above comment.

applicationWillTerminate works as long as I don't switch off the iPhone

to save some variables of my apps I use:
-(void)applicationWillTerminate:(UIApplication *)application {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setFloat: self.viewController.sLabel.contentOffset.y forKey:#"floatKey"];
[prefs setObject:self.viewController.newText forKey:#"stringVal"];
[prefs synchronize];
}
and to retrieve them, via a button, I do the following:
-(IBAction) riprendi:(id) sender {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
float myFloat = [prefs floatForKey:#"floatKey"];
//some actions here
}
Everything is working on the simulator. However, using it on a real iPhone, the variables' saving and retrieving works just if you press the Home button, exiting the app and opening again but NOT if you switch off/on the iPhone. In this case, the variables get simply lost once you re-open the app...
What am I missing?? This is actually driving me crazy :(
Thank you so much ;)
Fabio
If you mean hitting the lock button on the top of the phone by saying "switching on/off", then it won't work, because locking the phone does not cause an application to quit.
Your applicationWillTerminate: method is only called when you exit you're application to the home screen or to some other application. When the user presses the Sleep button, applicationWillResignActive: will be send to your application-delegate.
Apple's iPhone OS Programming Guide has a section on handling interruptions.
According to Apple's documentation
"NSUserDefaults caches the information to avoid having to open the user’s defaults database each time you need a default value."
If you want to make sure things are saved you should call "synchronize" on your prefs.