开发者

Confusion about session objects in Facebook Connect for iPhone

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], 开发者_Python百科but don't say where that session reference is supposed to come from. (And calling [[FBSession session] resume] compiles, but doesn't work.)

Thanks.


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.


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜