I've been toying with the new facebook iOS sdk. I have gotten my project to the point where someone can login successfully. However I have 2 questions:
1) to hit the graph api you issue the following call: [facebookInstance requestWIthGraphPath:@"me" andDelegate:self]. Is it possible to specificy a delegate other than self? Currently all responses go开发者_运维技巧 to the (void)request: (FBRequest *) request didLOad:(id) result. But since my app may issue requests to the facebook api at different times and need different things to happen for each respective request issued, how can I specifiy which callback function the response should hit in my app? Is this possible?
2) Once the user has logged in, how can you check their authorization/login status so that I can disable the login button if they are already logged in? Consider the example of a user turning on the app for the 1st time and logging in. Then closing the app, and opening a few minutes later. I rather not show the user the login button the 2nd time and instead start pulling information to display such as their name.
1) There shouldn't be anything wrong with specifying a delegate other than self, as long as the object you provide as the delegate conforms to the FBRequestDelegate protocol. Alternately you can interrogate the FBRequest object you get in the delegate method to determine which request it was that just loaded and what the appropriate response is.
2) To make it so the user stays logged in, you need to save the accessToken (an NSString) and the expirationDate (an NSDate) properties of the Facebook object when the user logs in. Then, when you would log your user in, attempt to restore these values. Some code snippets that may help:
- (void)fbDidLogin
{
NSString *tokenString = [[self facebook] accessToken];
NSDate * expirationDate = [[self facebook] expirationDate];
[[NSUserDefaults standardUserDefaults] setObject: tokenString forKey:@"FacebookToken"];
[[NSUserDefaults standardUserDefaults] setObject: expirationDate forKey:@"FacebookExpirationDate"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
And, when you need to log the user in:
NSString *tokenString = [[NSUserDefaults standardUserDefaults] objectForKey:@"FacebookToken"];
NSDate *expDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"FacebookExpirationDate"];
if (tokenString != nil && expDate != nil)
{
[facebook setAccessToken:tokenString];
[facebook setExpirationDate:expDate];
}
if ([facebook isSessionValid])
{
//Your session is valid, do whatever you want.
}
else
{
//Your session is invalid
//(either the session is past the expiration date or there is no saved data for the login)
//you need to ask the user to log in
}
精彩评论