How to log out from fbsession? - ios

H,In my app a user can login to the app using facebook.I am able to login successfully using fbsession but i am not able to logout form fbsession.Once the user login to the app i have do manually log out from fbsession. but i am not able to log out from fbseesion. In my app when ever i click on login button its not directing to login page its directly showing dialog page,where as for me i have to show login page every time in my app.
here is my code
- (IBAction)facebooklogin:(id)sender
{
[FBSession openActiveSessionWithReadPermissions:#[#"email",#"user_location",#"user_birthday",#"user_hometown"]
allowLoginUI:YES
completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
switch (state) {
case FBSessionStateOpen:
[[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
if (error) {
NSLog(#"error:%#",error);
}
else
{
// retrive user's details at here as shown below
NSLog(#"user :%#",user);
NSDictionary *resultdict=[[[NSDictionary alloc]initWithObjectsAndKeys:user.first_name,#"FirstName",user.last_name,#"LastName",user.last_name,#"LastName",user.birthday,#"Birthday",user.username,#"Username",[user objectForKey:#"email"],#"Email",user.id,#"FBUserId", nil]autorelease];
[[NSUserDefaults standardUserDefaults]setObject:resultdict forKey:#"FBUserDetails"];
NSString *userid=[NSString stringWithFormat:#"%##social",user.id];
[self performSelector:#selector(checkSocialNetworkingRegisteredOrNot:) withObject:userid afterDelay:0.0f];
}
}];
break;
default:
break;
}
} ];
}
/////////////////////////manually logout from fbsession ///////////
- (IBAction)logoutmanullayInbackground:(id)sender
{
[FBSession.activeSession closeAndClearTokenInformation];
[[FBSession activeSession] close];
[[FBSession activeSession] closeAndClearTokenInformation];
[FBSession setActiveSession:nil];
}

You are logging out (the fact you see the button say "Log in" again means you've successfully logged out). However, if you're using SSO (via either iOS integrated login, or the Facebook app), then when the user clicks "Log in" again, they won't be prompted (since the user on the App or device has already authorized your app).
To get around this (since you say you're making a restaurant app), you can uninstall the Facebook app (and remove the login from iOS), or you can log in with the FBSessionLoginBehaviorForcingWebView when you open the session.
See https://developers.facebook.com/docs/reference/ios/current/class/FBSession/#openWithBehavior%3AcompletionHandler%3A

Related

Facebook SDK native login error

Note: I am following the native login advice from Native Facebook Login stopped working after SDK update to 3.14.
The error is as follows:
2014-10-13 20:03:27.378 Registration[1916:407643] Error
Domain=com.facebook.sdk Code=9 "Access has not been granted to the
Facebook account. Verify device settings." UserInfo=0x1753c630
{NSLocalizedDescription=Access has not been granted to the Facebook
account. Verify device settings., NSLocalizedFailureReason=Access has
not been granted to the Facebook account. Verify device settings.}
This is the code below:
// RegistrationManager.m
- (void)setupFacebook
{
FBSessionStateHandler completionHandler = ^(FBSession *session, FBSessionState status, NSError *error) {
[self sessionStateChanged:session state:status error:error];
};
if ([FBSession activeSession].state == FBSessionStateCreatedTokenLoaded)
{
// we have a cached token, so open the session
[[FBSession activeSession] openWithBehavior:FBSessionLoginBehaviorUseSystemAccountIfPresent
completionHandler:completionHandler];
}
else
{
[self clearAllUserInformation];
// create a new facebook session
FBSession *fbSession = [[FBSession alloc] initWithPermissions:#[#"public_profile"]];
[FBSession setActiveSession:fbSession];
[fbSession openWithBehavior:FBSessionLoginBehaviorUseSystemAccountIfPresent completionHandler:completionHandler];
}
}
- (void)clearAllUserInformation
{
[FBSession.activeSession closeAndClearTokenInformation];
[FBSession renewSystemCredentials:^(ACAccountCredentialRenewResult result, NSError *error) {
NSLog(#"%#", error);
}];
[FBSession setActiveSession:nil];
}
// RegistrationViewController.m
- (IBAction)loginButtonPressed:(id)sender
{
// If the session state is any of the two "open" states when the button is clicked
if (FBSession.activeSession.state == FBSessionStateOpen
|| FBSession.activeSession.state == FBSessionStateOpenTokenExtended)
{
// Close the session and remove the access token from the cache
// The session state handler (in the app delegate) will be called automatically
[FBSession.activeSession closeAndClearTokenInformation];
// If the session state is not any of the two "open" states when the button is clicked
} else
{
FBSessionStateHandler completionHandler = ^(FBSession *session, FBSessionState status, NSError *error) {
[registrationManager sessionStateChanged:session state:status error:error];
};
// create a new facebook session
FBSession *fbSession = [[FBSession alloc] initWithPermissions:#[#"public_profile"]];
[FBSession setActiveSession:fbSession];
[fbSession openWithBehavior:FBSessionLoginBehaviorUseSystemAccountIfPresent completionHandler:completionHandler];
}
}
When I click on the button, it simply says
2014-10-13 20:03:29.560 Registration[1916:407643] Session closed
2014-10-13 20:03:29.561 Registration[1916:407643] User logs out.
2014-10-13 20:03:29.573 Registration[1916:407643] User logs out.
It means that the user once selected "Didn't allow" in the pop-up that asks for permission. If he has his Facebook account linked in Settings, from now on, every time he'll try to login it'll show this error. It won't fallback to Safari or anything, just show this error. The user needs to got into Settings -> Facebook and set the slider for your app to ON.

Logout from Facebook after a share with Facebook iOS SDK

I'm building an iPad app where the user can post an image to his/her timeline.
These iPads are rentable, so the user should be logged out after the share has been done.
I've implemented the iOS Facebook Login, so the user is redirected to Safari to login. After the share I clean up all the tokens and within my app the session is destroyed, but when I go to Safari afterwards the user is still logged in.
Is there a way to make sure the user is logged out everywhere?
Kind regards,
Frederik
I've done something similar in the past.
It was a flow where any user holding the iPad logs in with his or hers FB credentials, forcing the sdk to present a webview.
A quick look, this is the code I wrote for it:
(this code is 1.5y old)
FBSession *newSession = [[FBSession alloc] initWithPermissions:#[
#"user_about_me",
#"publish_actions",
#"user_birthday",
#"friends_birthday",
#"email"
]];
[FBSession setActiveSession:newSession];
[[FBSession activeSession] openWithBehavior:FBSessionLoginBehaviorForcingWebView completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
[FBSession setActiveSession:session];
if(error){
NSLog(#"Error opening session");
[self showLoginError:error];
return;
}
if(status == FBSessionStateOpen){
NSLog(#"FB session opened");
[self getMe];
}
}];
When the flow ends, or the user cancels the flow, I logs the user out as follows:
[[FBSession activeSession] closeAndClearTokenInformation];
[FBSession setActiveSession:nil];
UPDATE:
The code for the share dialog:
NSMutableDictionary *dialogParameters = [NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSString stringWithFormat:#"%# - Vote for us!",
teamName, #"name",
#"Campaign title", #"caption",
shareMessage, #"description",
shareTeamPageURL, #"link",
sharePictureURL, #"picture", nil];
[FBWebDialogs presentFeedDialogModallyWithSession:[FBSession activeSession]
parameters:dialogParameters
handler:nil];

Store Facebook User Datas Locally in iOS

I have two questions related in iOS vs Facebook API.
1) How to customise the facebook login view when user click the login btn to get user data.
2) In my application i want to use the facebook API. In this way i can able to retrieve the Users Data in this below method. But how can i save these users datas in my app locally.
Highly appreciated for your help !!!
[FBSession openActiveSessionWithReadPermissions:#[#"email",#"user_location",#"user_birthday",#"user_hometown"]
allowLoginUI:YES
completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
switch (state) {
case FBSessionStateOpen:
[[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error){
if (error) {
NSLog(#"error:%#",error);
}
else
{
// retrive user's details at here as shown below
NSLog(#"FB user first name:%#",user.first_name);
NSLog(#"FB user last name:%#",user.last_name);
NSLog(#"FB user birthday:%#",user.birthday);
NSLog(#"FB user location:%#",user.location);
NSLog(#"FB user username:%#",user.username);
NSLog(#"FB user gender:%#",[user objectForKey:#"gender"]);
NSLog(#"email id:%#",[user objectForKey:#"email"]);
NSLog(#"location:%#", [NSString stringWithFormat:#"Location: %#\n\n",
user.location[#"name"]]);
self.fbUserDataDict = [NSDictionary dictionaryWithDictionary:user];
}
}];
break;
case FBSessionStateClosed:
case FBSessionStateClosedLoginFailed:
[FBSession.activeSession closeAndClearTokenInformation];
break;
default:
break;
}
} ];
How about using something like Parse to store them locally ? You can also use SQLite. Though do you mind expanding a bit on what would you like to do ? Just store them locally ?

Login via facebook sdk in ios

I'm using facebook-ios-sdk-3.10, Using this SDK I'll login and fetch user details from FB Its working fine for me.
This is the code I'm uisng
- (IBAction)fbLogin_click:(id)sender
{
if (AppDelegate.fbsession.state != FBSessionStateCreated) {
// Create a new, logged out session.
NSArray *permissions = [NSArray arrayWithObjects:#"offline_access", #"email", #"publish_stream", #"read_stream",#"read_friendlists",#"manage_friendlists",#"friends_about_me",#"publish_actions", nil];
// create a session object, with defaults accross the board, except that we provide a custom
// instance of FBSessionTokenCachingStrategy
AppDelegate.fbsession = [[FBSession alloc] initWithAppID:nil
permissions:permissions
urlSchemeSuffix:nil
tokenCacheStrategy:nil];
}
FBSessionLoginBehavior behavior = FBSessionLoginBehaviorForcingWebView;
if (AppDelegate.fbsession.state != FBSessionStateCreatedTokenLoaded) {
// even though we had a cached token, we need to login to make the session usable
[AppDelegate.fbsession openWithBehavior:behavior completionHandler:^(FBSession *session,
FBSessionState status,
NSError *error) {
if (error)
{
NSLog(#"Error");
}
[self GetFBUserDetails];
}];
}
}
-(void) GetFBUserDetails
{
if (AppDelegate.fbsession.isOpen)
{
[HUD show:YES];
// fetch profile info such as name, id, etc. for the open session
FBRequest *me = [[FBRequest alloc] initWithSession:AppDelegate.fbsession graphPath:#"me"];
self.pendingRequest= me;
[me startWithCompletionHandler:^(FBRequestConnection *connection,
NSDictionary<FBGraphUser> *user,
NSError *error) {
// because we have a cached copy of the connection, we can check
// to see if this is the connection we care about; a prematurely
// cancelled connection will short-circuit here
if (me != self.pendingRequest) {
return;
}
self.pendingRequest = nil;
// self.pendingLoginForSlot = -1;
// we interpret an error in the initial fetch as a reason to
// fail the user switch, and leave the application without an
// active user (similar to initial state)
if (error) {
return;
}
[AppDelegate.fbsession closeAndClearTokenInformation];
[FBSession.activeSession close];
[FBSession.activeSession closeAndClearTokenInformation];
FBSession.activeSession=nil;
[self FacebookCustomerRegister:user];
}];
}
}
In such case some user create Facebook account and not very his account through email, when he try to login via my app it shows empty screen after click login button, there is no action further. how can I notify the user "You not yet verify your FB account" and it not return to my app. how can I fetch the response from there ?
can anyone help me for this ?
The email address you get from Facebook using the Graph API or a FQL query is a verified email. If an account hasn't verified it's email yet it's not possible to get it.
so when you fetch user info and if you are not getting the email when you have the permission to get then user is no verified and you can show an alert or any info to user about verifying the email and information
check more detail here Is it possible to check if an email is confirmed on Facebook?

Facebook SSO IOS do not return to app

We use Facebook SSO in our application. On clicking Facebook login app goes to Facebook app, displays login. But after login it won't return to app. But if a user is already logged in Facebook app, login button click switches to Facebook app and shows permission dialog. On clicking allow it returns to app. Is it the default behavior? Or is there any problem with my code or our Facebook app settings? Thanks in advance.
SHAppDelegate *appDelegate = [SHAppDelegate application];
NSArray * permissions = [[NSArray alloc]initWithObjects:FACEBBOK_PERMISSION, nil];
if (!appDelegate.session.isOpen) {
appDelegate.session = [[[FBSession alloc] initWithPermissions:permissions] autorelease];
if (appDelegate.session.state == FBSessionStateCreatedTokenLoaded) {
[appDelegate.session openWithCompletionHandler:^(FBSession *session,
FBSessionState status,
NSError *error) {
[self updateView];
}];
}
}
if (appDelegate.session.isOpen) {
[appDelegate.session closeAndClearTokenInformation];
} else
{
if (appDelegate.session.state != FBSessionStateCreated) {
// Create a new, logged out session.
appDelegate.session = [[FBSession alloc] init];
}
// if the session isn't open, let's open it now and present the login UX to the user
[appDelegate.session openWithCompletionHandler:^(FBSession *session,
FBSessionState status,
NSError *error) {
// and here we make sure to update our UX according to the new session state
[self updateView];
}];
}
I have the same issue. This is not a .plist trouble - without URLSchemes(with fbXXXXX) facebook app and safari never return to you application.
In this case, the Faccebook app does not return only when the user is not logged in it.
Safari handles this situation correctly. The same way a Facebook app does not return control if the user canceled the permissions confirmation.
In other situations, all right.

Resources