I have a class which makes use of the app delegate, specifically it has an NSUserDefaults property which is being updated from my class.
In my auth class implementation file I'd have :
+ (BOOL) checkUserDefaults {
AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
if([appDelegate.session objectForKey:@"UserID"] != nil) {
return TRUE;
} else {
return FALSE;
}
}
+ (void) syncUserDefaults : (NSString *) UserID {
AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
[appDelegate.session setObject:UserID forKey:@"UserID"];
[appDelegate.session synchronize];
}
How would I store this line of code:
AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
As a global (to this class), so that I can just write for example [app.session setObject...]?
I could make a function:
+ (AppDelegate *) app
{
return (AppDelegate *) [[UIApplication sharedApplication] delegate];
}
And do [[self app].session setObject...]
, but say if I'm referencing the "app" function numerous times that would be instantiating the AppDelegate numerous times wouldn't it?