I'm integrating facebook and I still don't see how the FBSession ties everything together.
This is how I think it works.
You need a new FBSession each app launch. (Is this true?). The FBSession also needs to be closed every time the app stops (if this is true, how do we start a new session without asking user to login again?)
As far as the session is concerned, [FBSession activeSession] is a global managed by the facebook sdk? Should we just use this as the default session anytime we want to pull data or check if a session is alive?
Assuming the above logic has worked out and you get the user id (fb id) then we feed that id to a FBImageView and we get a profile pic, but isn't this id changing every launch? So storing it on our server isn't helpful? Should we get the id every launch?
Please let me know what is right or wrong about the above statements, and an explanation of why?
Thank you.
I suggest you to use activeSession of FBSession. This way you don't have to create sessions yourself. Just use [FBSession openActiveSessionWithReadPermissions:...] method of FBSession for activating activeSession, and on successful opening, You could use FBSession.activeSession for your Facebook requests. Good luck!
PS>
In order to restore activeSession you can do:
if (![FBSession.activeSession isOpen]) {
[FBSession openActiveSessionWithReadPermissions:#[#"basic_info"] allowLoginUI:NO completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
}];
}
which will open the session if it has cached token information without showing login UI.
Related
every time you tap the facebook login button you are asked to accept the read permissions regardless of whether you just accepted them on your previous login attempt. you can accept the read permissions and land back in the app, only to logout, login, and be presented with the read permissions to accept yet again.
this issue did not exist prior to 3.5, 3.5.1, or the new 3.5.2. Due to this issue my application is stuck on the latest version of the pre v3.5 version of the sdk (3.2.1).
to me, this sounds like a bug in the facebook sdk/app but when i submitted this bug report it was marked as "triaged/low priority" and had the following note attached: "Thanks for the report. We are prioritizing other reports at the moment. You may want to check out http://facebook.stackoverflow.com/" so here i am.
my logic behind the assertion of it being a facebook server side thing is that we are making an auth request to facebook. it is within the facebook realm that they decide what permissions you have and have not accepted. how can it be a fault on my side? it is not the developer's job to tell facebook what their own user has accepted or not.
heres the code that i have behind the "Login with Facebook" button touch up event:
AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
if (appDelegate.session.state != FBSessionStateCreated) {
appDelegate.session = [[FBSession alloc] initWithPermissions:permissions];
}
[appDelegate.session openWithCompletionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
if([appDelegate.session isOpen]) {
//login ui flow finished here
}
}];
anybody have any thoughts?
In the last SDK, read and publish permissions are separated. So, in my code, whenever the users logs in I am using this code:
[FBSession openActiveSessionWithReadPermissions:nil
allowLoginUI:YES
completionHandler:
^(FBSession *session,
FBSessionState state, NSError *error) {
[self sessionStateChanged:session state:state error:error];
}];
I have seen in the facebook developer that when I want to publish, I should ask for publish permissions like this:
// can include any of the "publish" or "manage" permissions
NSArray *permissions =
[NSArray arrayWithObjects:#"publish_actions", nil];
[[FBSession activeSession] reauthorizeWithPublishPermissions:permissions
defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session, NSError *error) {
/* handle success + failure in block */
}];
Since this is my first app that I am using facebook integration, I want to ask a couple of things:
1) Can these sessions be open at the same time? I will ask for the publish permissions only when the user wants to publish, but should I do something else with the other session? Like close it first, and reopen it later or I should not worry about?
2) If I have a postToFriends button for example, my pseudocode for making it work would be like this, right?
- (IBAction)postToFriendaction:(id)sender {
if (!FBSession.activeSession.isOpen) {
/* code from the above for enabling publish permissions*/
}
or I should change !FBSession.activeSession.isOpen to something else, because no the user is logged in with read permission only, he never enters the if clause. Can you help me?
The two sessions you refer to are actually the same session (it's the "active" session that's statically available after a call to openActiveSession... is called). So you don't need to do anything with the other session.
You should have a look at either the Scrumptious or Hello Facebook sample apps that ship with the SDK. They both give examples on how to post (while asking for publish permissions).
I'm sure there's something simple I'm missing regarding this, but when using requestForUploadPhoto to send a photo to an album, it gets sent up with permission set to 'Only me'. Because the permission setting is so restrictive, photos then need to be manually approved by the user. Is there a way to alter the default upload permission (which will hopefully stop it requiring manual approval)? Normally it would be set when requesting a publish permission, but because user_photos is technically a read permission, I'm not given the chance to specify this. Code is below.
[FBSession openActiveSessionWithReadPermissions:[NSArray arrayWithObject:#"user_photos"] allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
FBRequest* request = [FBRequest requestForUploadPhoto:uploadImage];
FBRequestConnection* connection = [[FBRequestConnection alloc] init];
[connection addRequest:request completionHandler:^(FBRequestConnection* connection, id result, NSError* error)
{ NSLog(#"Done with upload"); }];
[connection start];
}];
I know it's not even slightly close in terms of catching and managing other session changes, but I'm not fussed with that atm - I just want to get this permission issue resolved before coding the rest.
user_photos only allows you to see photos (using GET), and does not allow you to post a photo. To do that, you need either publish_stream or publish_actions. Are you sure your app didn't already have a publish permission somehow?
You are correct in that the privacy is set when an app requests publish permissions, and the user can change that at any point later on as well (via the Facebook app or website).
I've been trying to integrate Facebook and came across some issues and questions.
1.
I have implemented the following method in viewDidLoad:
[FBSession openActiveSessionWithReadPermissions:nil allowLoginUI:NO completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
[self sessionStateChanged:session state:status error:error];
}];
When there's a token cached, the block is executed as expected.
But, when there isn't a token cached, the block isn't executed at all.
Is this a normal behaviour? If so, when is the block executed?
2.
I've been said that access tokens are expired at some point.
Should I be worried about what to do when a token is expired, or Facebook handles it automatically?
3.
What FBSession actually means?
As I understand - the FBSession manages the token and the user authentication,
Yet, I don't understand what does it mean to "open a session" or what the FBSessionStates represent.
When is the FBSession created? Right when the app loads?
4.
I've found that graph that should explain the FBSessionsStates:
After reading the graph, I still don't understand the flow - when are FBSessionStateCreated & FBSessionStateCreatedTokenLoaded the current state of the session?
As you can see, I have many questions about the whole process.
I've tried to organize the questions in a way that other people seeing this in the future, won't be having the struggles I'm having.
Thank you.
my app can only send app request during the first run that the user logins into facebook. Once the app is restarted, the app request gets stopped because FBSession.activeSession.isOpen gives false.
Although, FBSession.activeSession.isOpen gives false still the app can perform other operations such as posting to the wall, getting friend list etc. Only it cannot send app requests. And still it has a valid access token too.
Any ideas?
You say "FBSession.activeSession.isOpen gives false", but, just to be sure : when do you run this test? When your call to the login request returns, or inside the asynchronous request's callback ?
If it's done outside the request's callback, it may say that the session is not opened yet. Then, the request returns properly, which means that any further call to another FB request succeeds.
Does that help?
If it does not, maybe an overview of your code could.
Ok, next to your comment from the previous answer :
yes, that's what you said already. but WHERE in your code do you check isOpen? To expect a proper value, it should be something like this :
FBSession openActiveSessionWithReadPermissions:_readPerms allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
// here is the proper place to check the isOpen boolean, and to go on with your program, including the app request
}];
// not the proper place to check isOpen.
Does that help?