starting app from the same status as user had quit it - iphone

Suppose user is at any viewcontroller in app and filling the form the (view controller can be any viewcontroller).
Now he quits the app also from background.
So when user again starts the app i need to show the viewcontroller with same status he had quit it.

You'll have to save those field values - and what view is being shown - to some persistent storage (write to a plist for example)...depending on the layout of your app, you could do that each time the applicationWillEnterBackground, or applicationWillTerminate, or maybe every time textFieldDidEndEditing...then you'll need to recall that data on re-launch
http://www.edumobile.org/iphone/iphone-programming-tutorials/data-persistence-in-iphone/

Related

How to set First viewController Screen as a Default opening screen when ever open a app

Hi I am new to iPhone development, I am developing a TabBar app it's contains 5 tabbar Items.
When I am close app at third TabBar viewController screen and again when I am opening app that time same third TabBar viewController screen only opening.
How to set App first screen as a default App opening Screen in iphone.
I need to set first screen as a default opening screen when ever open a app.
Could you please share your ideas here..
When you close the app, it doesn't KILL it, it just suspends it. Unless you set the option to kill it when you close it.
I suggest you read Apple Documentation For Application Flow
And possibly read the other documents Apple have provided for you there if you are new to all of this.
In your application's Info.plist, add a boolean key UIApplicationExitsOnSuspend with the value YES.
This will always exit the app (instead of running in background) when you close it. On opening app again, first view controller will appear instead of where you left it last time.
This is because you are setting your application to be run in background, so whenever you close your app the app will run in background and resume on startUp.
so just do one thing
Go to your info.plist and set value for
Application does not run in background to YES
If application is killed by default it loads all settings in Application Delegate method:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
Here you can modify your root view controller, which I presume is some kind of tab bar view controller.
So there is probably some code in that method which defines which tab to load.
However if you want to show specific tab after application enters in the foreground use Application delegate method:
- (void)applicationWillEnterForeground:(UIApplication *)application
Or
- (void)applicationDidBecomeActive:(UIApplication *)application
When exiting the app by for example pressing the home button on the iPhone the app is not terminated. Instead the execution is suspended but the app will remain in memory until iOS decides to remove it. This means that when you enter the app again it will be in the same state you left it. You can see if the app is still in memory by doubletapping the home button.
When your app becomes active the method applicationDidBecomeActive: in your UIApplicationDelegate will be called. You can implement that method to set the state you want your app to be in when it is restored.
I would not recommend explicitly killing the app when it is suspended. That forces the app to be reloaded each time it is started which will degrade performance.
For more information see Apple's documentation on app states.

How to start the first view every time when app resumes?

I am using xcode4.2 and inside appDidFinishLaunching i am initializing navigationController with my first view and sets the rootViewController of app to navigationController.I want to show the first view every time when app resumes. How i can i do this?
The easiest way I can think of is not using backgrounding. If you set the UIApplicationExitsOnSuspend key in Info.plist, when the user press the home button, your app will quit and next time it is launched it will start over and not resumed from where it was.
Otherwise, you can define – applicationWillEnterForeground: and – applicationDidBecomeActive: to go back to your first view whenever the app is resumed. Have a look at the UIApplicationDelegate reference and Multitasking states.

How to get the view that is currently being displayed as an NSString?

I need to get the name of the view the user is currently viewing as an NSString, in order to save it so that I can return the user to this view if they exit my app. As of now, if a user exits my app on "View4" for instance my app will return them to "View1" if they launch it even immediately afterwards.
P.S. I know this is possible because many apps, such as tap tap revenge and angry birds do this. You can quit at any time and return, and the view and everything you were at will be saved.
Save the view name using NSUserDefaults. Then, when you app launches, try to retrieve the name from user defaults.

how to show the same view when the app is reopened

I have a tableview which shows a webview on clicking some row in the table, which in turn picks up the data from an sqlite. if a user closes the app by pressing home key while viewing a description in webview and reopens it after sometimes, I should be making the user to see the same screen. how to show the same view again ? What is the efficient way ?
Well I think the easiest way is to store the state of the application in NSUserDefaults. There is a delegate method on UIApplication called:
- (void)applicationWillTerminate:(UIApplication *)application
This delegate method gets called when the user quits the app. This is the time where you can save the state of your application off to the NSUserDefaults. But be aware that you cannot do time intensive stuff there. If you do, you get killed by the OS.
In your case why not simply store the row the user picked in NSUserDefaults and then check in
-(void)applicationDidFinishLaunching:(UIApplication *)application
if there is a saved row and restore the screen approbiately.
The easiest way is to write a string to a file in your documents directory that holds your state information in some easily parsed format. For example "screen_name:database_id". Or build a JSON string and parse it with JSON.
Everytime you switch screens, rewrite the file. When you open next time, read the file, parse it, and show the appropriate screen in your app.

How to Recreate the Last State of an App the Next Time it is Launched?

I have 2 views. When I'm on the 1st view, I have a textfield. I enter some text in the textfield. I exit the app, launch some another application and then return to my application. I want to maintain state of the textfield and that view. Likewise, if I quit with the 2nd view active, when I return to the app I need to return the view to the state it was in when the app quit.
How do I save the information about the state of my app when it last quit so I can recreate it upon the next launch?
Use the NSUserDefaults to write the value and selected view index to the user's preferences.
Apple's DrillDownSave sample may be helpful