Facebook SSO and request iOS SDK
I am trying to post a wall message to the current user. Before I am doing the request I am validating the session and authorizing if it isn't. I left out some of the declarations and the initializing of the variables since to clarify. The problem is that the code shown here is executed before the fbDidLogin-delegate method is executed. So it seems like the Facebook object doesn't have the valid access token before it is requesting. I have made a singleton. Next time I run the program it works.
Delegate
- (void)fbDidLogin
{
NSLog(@"DEBUG: DID LOG IN");
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
[defaults synchronize];
}
Viewcontroller
Facebook *facebook = [SocialMedia sharedFacebookAuthorized];
NSString *name = nil;
NSString *message = nil;
NSString *picture = nil;
if (self.didPassTest) {
// Code
} else {
// Code
}
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
name, @"name",
message, @"message",
picture, @"picture",
description, @"description",
link, @"link",
caption, @"caption", nil];
[facebook requestWithGraphPath:@"me/feed" andParams:params andHttpMethod:@"POST" andDelegate:self];
SharedFacebook..function
+ (Facebook *)sharedFacebookAuthorized
{
[self sharedFacebook];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"] && [defaults objectForKey:@"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
}
NSArray *permissions = [[NSArray arrayWithObjects:@"email", @"read_stream"开发者_StackOverflow社区, @"publish_stream", nil] retain];
NSLog(@"START?");
if (![facebook isSessionValid]) {
[facebook authorize:permissions];
NSLog(@"INSIDE?");
}
NSLog(@"CONTINUE?");
return facebook;
}
This is an asynchronous task. You have to wait for the authorize asynchronous call to complete before requesting anything from the graph API. See my response to a similar question here: How to react to asynchronous events (login)?
精彩评论