开发者

iphone session management

开发者 https://www.devze.com 2022-12-16 06:35 出处:网络
How can I manage user sessions in an iPhone app? I get a username and password from the user on the first page of my app. The user may log out whenever he wants. How can I store session information in

How can I manage user sessions in an iPhone app? I get a username and password from the user on the first page of my app. The user may log out whenever he wants. How can I store session information in an 开发者_StackOverflowiPhone app like any other web application? Is there any other technique for that? thank you.


You can call NSUserDefaults from anywhere in your app. This is usually where state information is stored.

If you envision that you might have to store information for a large number of users, you should create a custom database and only rely on . The easiest way to do that is to use Core Data. You can park the managed object context in the application delegate and then access that from anywhere by calling the app delegate.

I would caution you that an iPhone app has a different design pattern than a web app. You shouldn't be thinking in terms of pages and sessions unless you are implementing a web based interface.


you can save username information on NSUserDefaults

-(void)saveToUserDefaults:(NSString*)myString
 {
    NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];

    if (standardUserDefaults) {
        [standardUserDefaults setObject:myString forKey:@"username"];
        [standardUserDefaults synchronize];
    }
 }

if you have username information on NSUserDefaults, you can get your username information

-(NSString*)retrieveFromUserDefaults
 {
    NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
    NSString *val = nil;

    if (standardUserDefaults) 
        val = [standardUserDefaults objectForKey:@"username"];

    return val;
 }

using:

NSString * username=[self retrieveFromUserDefaults:@"username"];
if (!([username length]==0)||![username isEqualToString:@""]) 
{
NSLog(@"no authentication and redirect authentication page ");
}
else 
{
NSLog(@"it's authentication!");
}
0

精彩评论

暂无评论...
验证码 换一张
取 消