views:

196

answers:

2

Hi,

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 ?

A: 

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.

Vineel Shah
how to show the appropriate screen ? how to do that ? should the current view object be saved and show the same screen or should it be in some other way ?
thndrkiss
Why do parsing? You can store the objects directly in NSUserDefaults. For larger data amounts a file in your documents directory is a good solution, though.
GorillaPatch
using NSUserDefaults is a lot easier than reading/writing/parsing a file.
kubi
+3  A: 

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.

GorillaPatch
so next time i open the application i will validate the userdefaults for my parameter, and show this view. What happens to the other piece of my routine ? I do database loading, verfication of user data and other things. . . should i write the code to show the last displayed screen after doing all db stuff,loading views which are typically displayed ?
thndrkiss
Well in general there will be a controller object of some kind mediating the data from the tableView to the webView and to the database. In this controller object you will handle the restore.
GorillaPatch