简体   繁体   中英

How to handle session in facebook ios sdk

Hi I'm working on an app. In the app is possible to sing-in with facebook or not... And in several places of the app you can share stuff on you own timeline.

I followed the scrumptious example that facebook provides, and the log-in is working fine... But when I have to share stuff, I don't know if it's necessary or not to check if the session is active before calling the FBRequestConnection method to post to fb.

What's the best way to handle this? Because all the authentication is made calling an open session method in the delegate so... I have to call this methods from anywhere in the app? How do I go back to the place where I was before calling the authentication method... I'm sure there must be some kind of patron to use, but I can't find which one...

Any idea?

thanks!

Use a block of code like this to make sure that the session is open, which is important because if the user isn't, your app will crash. And then you need to have you OpenGraph objects within the FBRequestConnection.

    if (FBSession.activeSession.isOpen) {
        [[FBRequest requestForMe] startWithCompletionHandler:
         ^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
             if (!error) {
                 self.nameString = user.name;
                 self.profileImage.profileID = user.id;
                 self.userName = user.username;
                 [self.tableView reloadData];
             }
         }];
    }
}

** For your comment below: **

Okay, then perhaps bring up a UIAlertView asking the user to log into Facebook. If they select yes, then do something like

AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
[appDelegate openSessionWithAllowLoginUI:YES];

assuming that your AppDelegate looks like Scrumptious.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM