Skip to content
This repository

Support Request: maintaining a session? #66

Closed
utneon opened this Issue October 10, 2012 · 8 comments

4 participants

Paulo Carvalho Kyle Browning m1n0 paewrblue
Paulo Carvalho

How do I verify if a session is open in Drupal using the DIOS SDK? I don't find it in the documentation. I think that the documentation is also a bit outdated. Thank you.

Paulo Carvalho

I think I have found the solution:

Save the cookie session with login:

[DIOSUser userLoginWithUsername:[NSString stringWithFormat:@"%@", username.text] andPassword:[NSString stringWithFormat:@"%@", password.text] success:^(AFHTTPRequestOperation *operation, id responseObject) {
//successful
#ifdef DEBUG
NSLog(@"login successful: %@", responseObject);
#endif

    //If you have logged in, this object is not nil
    [session setUser:responseObject];

    // Guardar o cookie da sessão na aplicação.
    NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    NSArray *cookies = [cookieStorage cookies];
    for (NSHTTPCookie *cookie in cookies) {
        if ([cookie.name hasPrefix:@"SESS"]) {
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            [defaults removeObjectForKey:@"cookiename"];
            [defaults removeObjectForKey:@"cookievalue"];
            [defaults setObject:cookie.name forKey:@"cookiename"];
            [defaults setObject:cookie.value forKey:@"cookievalue"];
            [defaults synchronize];
        }
    }

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    //failed

#ifdef DEBUG
NSLog(@"login failed");
#endif
}];

When app loads send the session cookie in the Header:

// Ir buscar os dados da última sessão através do NSUserDefaults.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSString *sessionName = [defaults objectForKey:@"cookiename"];
NSString *sessionValue = [defaults objectForKey:@"cookievalue"];

// Enviar os cookies da sessão no header da sessão.
if (![sessionName isEqualToString:@""] && ![sessionValue isEqualToString:@""]) {
    [session setDefaultHeader:@"Cookie" value:[NSString stringWithFormat:@"%@=%@", sessionName, sessionValue]];
}

// Testar a conexão ao services Drupal
[DIOSSystem systemConnectwithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

#ifdef DEBUG
NSLog(@"success: %@", responseObject);
#endif
// Definir o user da sessão.
[session setUser:responseObject];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
#ifdef DEBUG
NSLog(@"not success %@", operation);
#endif
}];

Is this correct? -> [session setUser:responseObject]; or should it be [session setUser:[responseObject objectForKey:@"user"]];?

Kyle Browning
Owner

You shouldnt have to sore the cookies at all, AFNetworking handles all of that and will resume the session until the cookies expire.

Kyle Browning kylebrowning closed this October 15, 2012
m1n0
m1n0 commented March 26, 2013

@kylebrowning
Ok you say that we shouldnt take care of cookies...but would you care to show an example how to solve the issue above?

Kyle Browning
Owner

@m1n0 What do you mean? Basically you can store the user information returned from user Login by doing this.

[session setUser:userDictInfo];

But you must keep in mind if you used responseObject it wont be retained properly.

If you dont want to store that information thats okay, AFNetworking will keep the cookie until it expires. If you want to validate a user is logged in, you can use the system Resource and call 'connect'

Does that help?

m1n0
m1n0 commented March 26, 2013

Thanks, yes that is exactly what I need, I am just not sure if I know what you mean by "...you can use the system Resource and call 'connect'"

Kyle Browning
Owner

https://github.com/workhabitinc/drupal-ios-sdk/blob/master/DIOSSystem.m

You dont HAVE to do it with System, if you wanted you could use userLogin again and you should get a failure saying the user is already logged in.

m1n0
m1n0 commented March 26, 2013

Ok, thanks a lot!

paewrblue

How can I apply session to next ViewController after logging ?

I use this method, and I got null out of every ViewController, and please tell me where to put the code, I'm very new to IOS.
DIOSSession *session = [DIOSSession sharedSession];
//If you have logged in, this object is not nil
[session user];
//This can also be referenced as
[[DIOSSession sharedSession] user];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.