views:

715

answers:

2

I've done a lot of reading of the Facebook docs, but I'm pretty confused about the role of the session object. For instance, with the method:

session = [FBSession sessionForApplication:myApiKey secret:myAppSecret delegate:self];

What am I supposed to do with the session object that's returned to me, when presumably I need to wait for the delegate callback in order to do anything?

Secondly...upon a later execution of my app, when the user has authorized me accessing their account during a previous execution, how do I get a reference to a session object so I can connect to their Facebook account and post status, etc.? The docs mention [session resume], but don't say where that session reference is supposed to come from. (And calling [[FBSession session] resume] compiles, but doesn't work.)

Thanks.

+2  A: 

What am I supposed to do with the session object that's returned to me, when presumably I need to wait for the delegate callback in order to do anything?

You have to create and show a FBLoginDialog to the user. The delegate method will not be called until the user has logged in. It has nothing to do with creating the session instance in the first place.

how do I get a reference to a session object so I can connect to their Facebook account

The Facebook Connect library will store the session info in your app's user defaults. You don't have to do anything to store it. As I understand it, on every launch of your app you should create the session object with +sessionForApplication:secret:delegate: and then call [session resume]. If the FBConnect library finds a valid session stored in the user defaults, it will return YES and you can proceed as the user is now logged in. If resume returns NO, you will have to show the login dialog.

From Facebook's docs:

The session information will be stored on the iPhone disk in your application's preferences, so you won't have to ask the user to log in every time they use your application. After you've created your session object, call [session resume] to resume a previous session. If the session has expired or you have yet to create a session, it will return NO and you will have to ask your user to log in. Sessions expire after two hours of inactivity.

Ole Begemann
You said, "As I understand it, on every launch of your app you should create the session object with +sessionForApplication:secret:delegate: and then call [session resume]." My question: is the valid session the return value from this method, or the session object that is passed to the delegate? If it's the former, why do I need a delegate? (Passing nil as the delegate causes it to crash.) And if it's the latter, what is the point of the returned session object? Thanks.
Greg Maletic
Both are the same object and thus equally valid. It's customary for delegate methods to include the delegating object as a parameter so that the programmer can identify which object the delegate message is about. As to your question, "why do I need a delegate?": Because creating a session is not the same as logging in. As I said, after creating the session object, you need to call the FBLoginDialog and then FBSession will use the delegate method to inform you when the user has actually logged in. At that point, you can continue using the session.
Ole Begemann
Thanks...but what if I know the user has already logged in? I shouldn't need to show a log in dialog then. I don't want to have that Facebook dialog popping up every time (if I call FBDialog, if the user is already logged in, it pops open, then closes, which is distracting.)
Greg Maletic
I have already talked about that in my answer.
Ole Begemann
+1  A: 

Hi Greg, this is really following on from Ole's answer and comments as it is all in there. This is what I did.

Firstly create the session object:

facebookSession = [[FBSession sessionForApplication:SESSION secret:SECRET delegate:self]retain];    
[facebookSession resume];

The second line pulls the session data from the settings file to keep you logged on if the user specifies that they'd like to be kept logged on.

You have to implement the method:

- (void)session:(FBSession*)session didLogin:(FBUID)uid

although I don't use it for anything.

To check if the user is already logged on, I call:

[[FBSession session] isConnected]

and then either display the login dialog or straight to the publish dialog if they are already logged on.

Magic Bullet Dave