开发者

facebook isSessionValid is always NO

hi i use the newest facebook api with SSO and do implement like following in my viewController (not in the appDelegate like in the example if t开发者_如何学编程hat makes any difference): in my .h file

Facebook *facebook;
@property (nonatomic,retain) Facebook *facebook;

in my .m file i do the following in my viewDidLoad

self.facebook = [[Facebook alloc] initWithAppId:@"myappID"];

in another method I do

if (![facebook isSessionValid]) {


NSArray *permissions =  [[NSArray arrayWithObjects:@"publish_stream",nil] retain];
[facebook authorize:permissions delegate:self];
}

but it seems that isSessionValid is alway NO and I don't know why! does anybody have any idea why ? oh and also expirationDate and accessTokken are nil


I had this exact same problem yesterday, it seems you must persist and restore the access token/expiration date yourself.

In your viewDidLoad:

// Restore previously saved Facebook credentials (If any)
facebook.accessToken    = [[NSUserDefaults standardUserDefaults] stringForKey:@"FBAccessToken"];
facebook.expirationDate = (NSDate *) [[NSUserDefaults standardUserDefaults] objectForKey:@"FBExpirationDate"];

// Trigger SSO Facebook authentication if required
if ([facebook isSessionValid] == NO) {
    [facebook authorize:nil delegate:self];
} else {
    [self fbDidLogin];
}

Then in the fbDidLogin delegate:

- (void)fbDidLogin

{ // Save the users access token and expiration date so we can restore it later // This way we can avoid needlessly authenticating to Facebook.

[[NSUserDefaults standardUserDefaults] setObject:self.facebook.accessToken forKey:@"FBAccessToken"];
[[NSUserDefaults standardUserDefaults] setObject:self.facebook.expirationDate forKey:@"FBExpirationDate"];

// User has logged in to Facebook, now get their userId from Facebook
[facebook requestWithGraphPath:@"me" andDelegate:self];

}

So now users will have to login/authorize your application once and from then on their session will remain valid until they logout of Facebook.


alright here is the answer to my problem. as I said in the comment also the application:handleOpenURL: never got called! cause I had this method in my ViewController and apparently it has to be in the appDelegate!

so in my Navigationbar based application I did the following in my appDelegate:

in the .h file:

ViewController *viewController;
@property (nonatomic, retain) IBOutlet ViewController *viewController;

in the .m file:

@synthesize viewController;

viewController = [[ViewController alloc]init];
[self.navigationController pushViewController:viewController animated:NO];

and then implemented the method like that:

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {

return [viewController.facebook handleOpenURL:url]; 
}

and of course in my ViewController I did everything @Tyler mentioned (thank you for that) and everything works like a charm. hope this helps everyone with the same problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜