Maintain Session State in IPhone Application - iphone

HI, I want to maintain the session in my iphone application. If the application enter in to background state or application is not active i have to move the user to loging screen. How to do this one?
I saw this example but it is not help me to resolve the issue:
How to maintain Session for iphone

You can follow similar concept. When user logging in put some thing as NSUserDefaults like
[[NSUserDefaults standardUserDefaults] setValue:#"ABCDEFGHIJK" forKey:#"SessionKey"];
and in application delegate there is one method called
(void) applicationDidEnterBackground:(UIApplication *)application
which will be called when application is going in background. Just remove that set variable for the sessionkey. And while application is again being launched just check the condition and redirect user to desired screen.
Hope this helps.

Related

iPhone app login issue

I have an iPhone application which I'm working on and so far i have created a login page for the application such that only the phone's user can access the application. The login works fine and the application works fine, but when i hit the home button, the app is "minimized". And if accessed again from the task switcher it doesn't prompt for a password.
What exactly would be the best way to go about getting the app to request a password. If it helps i use navigation controllers and i have one view dedicated for the login.
Thanks for the help.
Can't you check app activated in the appdelegate?
- (void)applicationDidBecomeActive:(UIApplication *)application {
[self showLoginWindow];
}
You must be working on iOS4. When you hit on Home Button, your app goes into background mode, but still working. So next time, when you open it, it starts exactly from where you left it. Implement the
- (void)applicationDidBecomeActive:(UIApplication *)application
method, to check for login again. Or if you don't want the background processing at all, disable it or use following code
- (void)applicationDidEnterBackground:(UIApplication *)application {
exit(0);
}
EDIT -
Don't go for exit(0) approach, simply disable the background processing if you don't want it from info.plist file.
see this link
you need to prevent the app to load from background.

Remember Apps last screen in iPhone app

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.

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.

Awake from sleep event on the iPhone?

Is there any way to detect if the iPhone wakes up from sleep while you're app is running? Eg: your app is running, the user locks the screen (or the screen auto locks) and some time later the user unlocks the screen and up pops your app. Is there some way to get an event at that point or detect it somehow?
I've tried searching the Google and this forum, but I can't seem to find anything about it.
See applicationDidBecomeActive: on UIApplicationDelegate.
Stick these in you AppDelegate.m file:
-(void) applicationWillResignActive:(UIApplication *)application {
NSLog(#"Asleep");
}
-(void) applicationDidBecomeActive:(UIApplication *)application {
NSLog(#"Awake");
}
#Kevin - Nothing wrong with your answer - thanks by the way. Just thought I'd save the next person a Google search.

Problem with applicationShouldTerminate on iPhone

I'm having a problem with applicationShouldTerminate.
What ever I do it seams that has no effect. Any help would be
appreciated.
I'm well versed in programing but this just gives me headache. Im going
over some basic tutorials for xcode , as I'm new to mac in general, and am currently looking at a simple flashlight app.
It exists but I would like to add a alert box here with option not to
quit.
(void)applicationWillTerminate:(UIApplication *)application
{
[application setIdleTimerDisabled:NO];
}
this has no effect, alert is closed even before its created.
(void)applicationWillTerminate:(UIApplication *)application
{
[application setIdleTimerDisabled:NO];
UIAlertView *alertTest = [[UIAlertView alloc]
initWithTitle:#"This is a Test"
message:#"This is the message contained
with a UIAlertView"
delegate:self
cancelButtonTitle:#"Button #1"
otherButtonTitles:nil];
[alertTest addButtonWithTitle:#"Button #2"];
[alertTest show];
[alertTest autorelease];
NSLog(#"Termination");
}
I did some reading online and found that it should be possible to do
this with
(NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication*)sender
But no mater where I put that declaration I get error: syntax error
before NSApplicationTerminateReply.
There is no syntax error except that xcode seems not to recognize
NSApplicationTerminateReply as valid input.
Any sample code would be greatly appreciated.
I know this is a non-answer, but hopefully I can be helpful:
Displaying a "Really quit?"-type alert like this, even if you can pull it off technically (and I'm not sure you can), is a bad idea and is likely to either cause rejection from the App Store or, at best, an inconsistent user experience because no other apps do this.
The convention with iPhone apps is to save state if necessary, then yield control (for termination) as quickly as possible when the user hits the home button or switches apps.
To ensure a consistent experience, Apple probably has an aggressive timer in place to restrict what you can do in applicationWillTerminate. And even if they don't have a technical measure in place, they probably have an App Store approval policy to ensure that applications quit immediately when they're asked to.
applicationShouldTerminate and NSApplication do not exist on the iPhone. You have to use UIApplication.
The alert view is never shown because the 'show' method does not block, and therefore, the end of 'applicationWillTerminate' is reached immediately after you create the alert view and try to show it. I believe this is by design. You can't really begin asynchronous operations in 'applicationWillTerminate'.
With regards to the applicationShouldTerminate error, in case anyone's curious, NSApplicationTerminateReply and NSApplication seem to be deprecated...even though the OP's method is exactly how it appears in the docs!
Defining your method as the below should build with no errors:
-(BOOL)applicationShouldTerminate :(UIApplication *)application
I think I found the answer to what I wanted to do but will need to check it when I get back home.
Some directions were found here
http://blog.minus-zero.org/
The iPhone 2.0 software was recently released, and with it came the
ability for users to download native apps (i.e., not web sites)
directly to their phones from within the iPhone UI or via iTunes.
Developers (anyone who pays Apple 59GBP for the privilege) can then
write their own apps and have them available for purchase in the App
Store.
One limitation of the Apple-sanctioned SDK is that only one
application is allowed to be running at a time. This presents a
problem for apps such as IM clients, music players and other programs
whose functionality relies on being able to run in the background.
Another example (courtesy of James) would be an app that takes
advantage of the iPhone 3G's GPS chip to create a log of all the
places you visit.
However, there is a neat trick that I discovered: your app will only
get terminated if you switch away from it, and hitting the iPhone's
power button while your app is in the foreground doesn't count as
switching away. The upshot of this is you can create apps which
continue to run while the iPhone is in your pocket - perfect for the
GPS example.
Achieving this is as simple as implementing two methods in your
UIApplication delegate - applicationWillResignActive: and
applicationDidBecomeActive:. Here's a simple example to demonstrate
the effect.
In your UIApplication delegate header file, add a new ivar: BOOL
activeApp. Then, in your implementation, add the following three
methods:
- (void)applicationWillResignActive:(UIApplication *)application {
NSLog(#"resigning active status...");
activeApp = NO;
[self performSelector:#selector(sayHello) withObject:nil afterDelay:1.0];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
NSLog(#"becoming the active app...");
activeApp = YES;
}
- (void)sayHello {
NSLog(#"Hello!");
if (!activeApp)
[self performSelector:#selector(sayHello) withObject:nil afterDelay:1.0];
}