1
votes

I have declared a variable NSString sessionId; in app delegate file for global usage.

In appdelegate.h

@property (strong, nonatomic) NSMutableString *sessionId;

In appdelegate.m

@synthesize sessionId;

And used it in one of my view controller like this.

    ((VektorAppDelegate *)[UIApplication sharedApplication].delegate).sessionId = sID.retain;

NSLog(@"session id = %@", ((VektorAppDelegate *)[UIApplication sharedApplication].delegate).sessionId);

I am able to accèss that variable sessionId in all of my view controllers but the issue is that the value is not getting saved in it. Like in viewcontroller1.m, I assigned it some value but when I accessed sessionId in another viewcontroller2.m, its showing its value as null on console.

How can I resolve this issue.

Code added:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
     NSString *responseString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];

self.receivedData = nil;


    NSError *error;

    SBJSON *json = [[SBJSON new] autorelease];

    NSArray *session = [json objectWithString:responseString error:&error];

    SBJsonParser *parser =[[SBJsonParser alloc] init];

    NSDictionary *jsonObject = [parser objectWithString:responseString];

    sID = [jsonObject objectForKey:@"session"];

    ((VektorAppDelegate *)[UIApplication sharedApplication].delegate).sessionId = sID;

    NSLog(@"session id = %@", ((VektorAppDelegate *)[UIApplication sharedApplication].delegate).sessionId);


    NSUserDefaults *sessID = [NSUserDefaults standardUserDefaults];

    [sessID setObject:sID forKey:@"sessionID"];

    [sessID synchronize];

    NSLog(@"This session %@ saved", sID);

    NSDictionary *accountsObj = [parser objectWithString:responseString];

    NSString *accounts = [accountsObj objectForKey:@"account"];

    NSArray *accountList = [accounts componentsSeparatedByString:@","];

    NSLog(@"Accounts: %@", accountList);


    if (session == nil)
    NSLog(@"JSON parsing failed: %@", [error localizedDescription]);
else {

    if (accountList.count > 1) {

        ChooseAccountsController *accountsView = [[ChooseAccountsController alloc] initWithNibName:@"ChooseAccountsController" bundle:nil ];

        self.chooseAcc = accountsView;

        [self.view addSubview: accountsView.view];
    } else if (accountList.count == 1) {

            MainScreenController *mainScreen1 = [[MainScreenController alloc] initWithNibName:@"MainScreen" bundle:nil ];

             self.mainScreen = mainScreen1;

             [self.view addSubview: mainScreen1.view];

         }
    }

}
4
Well, show us how you did declare the variable in the first place, please. - cli_hlt
man, your variable naming is horrible! why do you assign your user defaults to an name sessID? - vikingosegundo
before getting into application delegate variable stuff, I was thinking of nsuserdefaults to save the sessionId, thats y - user831679
I don't see, why you need this property on your AppDelegate. Just write and read it to/from your [NSUserDefaults standardUserDefaults] where needed. - vikingosegundo
was just gonna try it... but didn't used it... now I have deleted it .. if the solution you have suggested will work fine then I don't need another method ... but still I am getting issue of variable values getting null sometimes and sometimes not ... this abnormal behavior is very bad .. - user831679

4 Answers

0
votes
((VektorAppDelegate *)[UIApplication sharedApplication].delegate).sessionId = sID.retain;

is not valid code, try

((VektorAppDelegate *)[UIApplication sharedApplication].delegate).sessionId = sID

a strong property should be retaining, and also retain is a message, so if you need this (not in this case) the only correct syntax is [anObject retain]

try

((VektorAppDelegate *)[UIApplication sharedApplication].delegate).sessionId = [jsonObject objectForKey:@"session"];
0
votes

Honestly dont know why this not work, but try to create a Singleton Object to save and Access this Variable. This is more elegant and should serve your Intent.

0
votes

NSLog its value in viewcontroller1 to make sure its set.

If it is, then it is most likely that in viewcontroller2 you are redefining the sessionid when you try to access it.

Also you may want to create a singleton outside of the AppDelegate.

0
votes

I think the line

((VektorAppDelegate *)[UIApplication sharedApplication].delegate).sessionId = sID.retain;

should read

((VektorAppDelegate *)[UIApplication sharedApplication].delegate).sessionId = sID;

I wonder that it worked at all without crashing...