Ricardo Macario
Ricardo Macario

Reputation: 213

Using cookies on iOS application with AFnetworking

I'm building an iOS application which talks to a php web service. The web service also serves a web application. So there is a simple register and login functionality that uses a username and password, on the web application side when you log in you have the option to click remember me, which stores a cookie that last 1 year so you don't have to log in every time you visit the page.

Now, on to the iOS part. The first time the user opens the app he is required to enter his credentials, this sends a post call to the web service, since its and iOS applications it asumes the remember me is clicked, so it returns the cookie to avoid asking the credentials again.

I've read that some people save the cookies on the user defaults and restores them every time the app is launched. But I've tested some times and apparently this cookie is saved magically without me writing any code, I even turned of the phone launch the app and the web service still remembered me.

So, do I have to save the cookies? is it necessary ? Should I improve the security? if so how?

Upvotes: 1

Views: 1162

Answers (2)

Wain
Wain

Reputation: 119031

As you have observed, the cookies are stored for you and will 'generally' respect the specified cookie policy and timing as returned from the server. It isn't 100% reliable though and in some cases the cookie could be removed before the set date (probably not going to happen often). If you're happy to take the 'risk' that the user may have to re-login sometimes then you don't need to do anything. If you want to be sure about the management then you should use NSHTTPCookieStorage to get the cookies, store them somewhere else (like NSUserDefaults) and restore them later.

Upvotes: 1

Vivek Sehrawat
Vivek Sehrawat

Reputation: 6570

There is no such magic which can save data in NSUserdefaults, i can give u some idea how u can do it. first u have to take a bool which can store that if u have clicked the remember me button. if the user have clicked it then the check bool is true and when the user will login it will check the bool if true then it will save the data in NSUerdefault.

whenever the application will launch then first it will check that the bool inside the NSUserdefault is true or not and than if true it will hit the service and login neither it will navigate to the login screen. you can modify the below code more as per your requirments

-(IBAction)remember{
    check=TRUE;
}
-(IBAction)Login{

    if (check) {
        NSLog(check ? @"True" : @"False");

        NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
        [defaults setBool:TRUE forKey:@"data"];
        [defaults setObject:emailStr forKey:@"email"];
        [defaults setObject:passStr forKey:@"pass"];
        [defaults synchronize];
        NSLog(@"Data saved");
    }
//write here code for sending the request for login 
}

In Appdelegate write this 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    if(![[NSUserDefaults standardUserDefaults] boolForKey:@"data"])
    {
        [self displayLogin];
    } else
    {
        [self displayMainScreen];
    }
       [self.window makeKeyAndVisible];
    return YES;
}
-(void)displayLogin{
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    UINavigationController *navigation=[[UINavigationController alloc]initWithRootViewController:self.viewController];
    self.window.rootViewController = navigation;
}
-(void)displayMainScreen{

    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    NSString *emailStr = [defaults objectForKey:@"email"];
    NSString *passStr = [defaults objectForKey:@"pass"];
//write here code for sending the request for login 
}

Upvotes: 3

Related Questions