I'm new in iOS programming and I am very interested in what singleton class is, and why it is used. I found some information but it is vague. In particular I would like to apply it to real example. My project use Facebook SDK and I want to create singleton class for my NSDictionary which contain friends list. My .m delegate file:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//here is some other code
facebook = [[Facebook alloc] initWithAppId:@"my app id" andDelegate:self];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"]
&& [defaults objectForKey:@"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
}
if (![facebook isSessionValid]) {
NSArray *permissions = [[NSArray alloc] initWithObjects:
@"user_location",
@"friends_location",
@"read_friendlists",
nil];
[facebook authorize:permissions];
[permissions release];
}
[facebook requestWithGraphPath:@"me/friends" andDelegate:(id)self];
//here is some other code
}
And I set to my NSDictionary value of request which return friends list:
- (void)request:(FBRequest *)request didLoad:(id)result {
_friendsDictionary = result;
}
In which way I need to write singleton class and do not use delegate class AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
.