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];
}
}
}
is not valid code, try
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