Logout from Facebook after a share with Facebook iOS SDK - ios

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];

Related

iOS Facebook SDK - requesting permission - opens with web fallback

Probably I am overlooking something, but I cannot find where I can set the mode in which the extra permissions are requested from the user with the iOS Facebook SDK.
I am requesting additional publishing permissions with the code from below (and yes, I've set facebookLoginView.loginBehavior = FBSessionLoginBehaviorUseSystemAccountIfPresent; for the login), but the extra permission request doesn't pop up in the app, instead it opens up the facebook application. Any idea how to use the built in popup for this request?
if ([[FBSession activeSession] isOpen])
{
NSArray *permissions = [[FBSession activeSession] permissions];
NSArray *newPermissions = #[#"publish_stream"];
if (![permissions containsObject:#"publish_stream"])
{
[[FBSession activeSession] requestNewPublishPermissions:newPermissions defaultAudience:FBSessionDefaultAudienceEveryone completionHandler:^(FBSession *session, NSError *error) {
if (error == nil)
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"FacebookWritePermissionsGranted" object:nil userInfo:nil];
}
}];
}
}

How to log out from fbsession?

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

Error code 5 when publishing Facebook Open-Graph story through iOS?

I have been trying to publish a FB Opengraph story through xcode and havent been able to do so yet.
The url is a static url, with all the meta tags set in the html file itself.
NSMutableDictionary<FBGraphObject> *action = [FBGraphObject graphObject];
action[#"testaction"] = #"http://myfbapp.com/fbobject.html";
[FBRequestConnection startForPostWithGraphPath:#"me/myfbapp:publish"
graphObject:action
completionHandler:
^(FBRequestConnection *connection, id result, NSError *error) {
}];
The code has been taken straight from the facebook app itself.
Thanks for any help!
Before calling startForPostWithGraphPath,
make sure the session is open:
NSLog(#"activeSession isOpen: %d", [[FBSession activeSession] isOpen]);
make sure the session has 'publish_actions' permissions:
NSLog(#"session perms: %#", [[FBSession activeSession] permissions]);
if not,
if ([FBSession.activeSession.permissions indexOfObject:#"publish_actions"] == NSNotFound) {
then ask for permission:
if ([FBSession.activeSession.permissions indexOfObject:#"publish_actions"] == NSNotFound) {
defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session, NSError *error) {
The problem was that the action had not been reviewed by FB and was in test mode. The account I was trying to post with was not the developer account linked with the FB App and therefore I was unable to post the action.
Once I logged in with my dev account, it was working fine.

I Only Get Facebook Active Access Token When Using iOS Facebook Integration

So when I am logged in to ios's facebook integration, my code works great. I get an active access token with permissions to read and write, and I have actually written to people's walls and whatnot from the app. However, when my app uses safari to authenticate people's login credentials, there is the common error: "An active access token must be used to query information about the current user."
What I don't understand is why I can only get my access code from the ios facebook integration. Anyways, my relevant code below is implemented by the current view controller when it loads:
if (![FBSession activeSession].isOpen) {
[self openSession];
}
my method openSession is defined as follows
- (void)openSession
{
//create the permissions array
NSArray *permissions =
[NSArray arrayWithObjects:#"email", #"basic_info", nil];
[FBSession openActiveSessionWithReadPermissions: permissions
allowLoginUI:YES
completionHandler: ^(FBSession *session, FBSessionState status, NSError *error) {
NSLog(#"\n\nopenSession called error = %#\n\n\n",error);
}];
}
I then go on to continue in the viewDidLoadMethod
[FBRequestConnection
startForMeWithCompletionHandler:^(FBRequestConnection *connection,
id<FBGraphUser> user,
NSError *error)
{
NSLog(#"error requestion connection: %#",error);
}];
I look forward to the response. Thanks in advance.
Using Facebook SDK.
NSString *fbAccessToken = [[[FBSession activeSession] accessTokenData] accessToken];
If you prefer dot syntax,
NSString *fbAccessToken = [FBSession activeSession].accessTokenData.accessToken;`
I hope , it will work for u

Not able to post to friends wall with facebook sdk in ios

I have tried with openGraph and normal dialog as below,
[facebook requestWithGraphPath:[NSString stringWithFormat:#"/%#/feed",#"friends_fb_id" ] andParams:variables andHttpMethod:#"POST" andDelegate:self];
and
[facebook dialog:#"feed" andParams:params andDelegate:self]
I gave params for both methods as
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"friends_fb_id", #"to",
#"I'm using the Hackbook for iOS app", #"name",
#"Hackbook for iOS.", #"caption",
#"Check out Hackbook for iOS to learn how you can make your iOS apps social using Facebook Platform.", #"description",
#"http://m.facebook.com/apps/hackbookios/", #"link",
#"http://www.facebookmobileweb.com/hackbook/img/facebook_icon_large.png", #"picture",
nil];
in delegate method it correctly return the Post_id, so its completing successfully but I can not see the post on friends wall. If try using the Hackbook app that comes with SDK it works perfectly.
What could be the possible solution.
Did you forget to enable publish_actions or publish_stream permissions during login?
// login to FB
-(void) FBLogin
{
NSArray *permissions = [[NSArray alloc] initWithObjects:#"publish_actions",#"publish_stream", nil];
[FBSession openActiveSessionWithPermissions:permissions allowLoginUI:YES completionHandler:^(FBSession *_session, FBSessionState _status, NSError *_error) {
// do some login handling code here
}];
}
You are using the string "friends_fb_id" should this not be an actual id?
I had a similar situation where is was receiving the correct response, so it was posting, it was just posting to the wrong place.
Make sure the id's are correct.

Resources