Log out from facebook like in native app - ios

My application supports login with facebook, but when I log out and log in again I am not asked for any credentials - it immediately logs in with the username which goes native in iOS6. I want something like in native facebook application:
When clicking "not you" facebook offers new login screen.
I followed this tutorial
The problem seems to be somewhere here:
- (void)openSession
{
NSArray *permissions=[NSArray arrayWithObjects: #"publish_stream", #"publish_actions",#"create_event", nil];
// [FBSession open]
[FBSession openActiveSessionWithPublishPermissions:permissions
defaultAudience:FBSessionDefaultAudienceEveryone
allowLoginUI:YES
completionHandler:
^(FBSession *session,
FBSessionState state, NSError *error) {
[self sessionStateChanged:session state:state error:error];
}];
}
- (void)sessionStateChanged:(FBSession *)session
state:(FBSessionState) state
error:(NSError *)error
{
switch (state) {
case FBSessionStateOpen: {
NSLog(#"Sesstion opened");
UIViewController *topViewController =
[self.navController topViewController];
NSLog(#"Class: %#", [[topViewController presentedViewController] class]);
if ([[topViewController presentedViewController]
isKindOfClass:[LoginViewController class]])
{
[topViewController dismissViewControllerAnimated:YES completion:nil];
}
ViewController *basic=(ViewController*)topViewController;
NSLog(#"Populating details");
[basic populateUserDetails];
}
break;
case FBSessionStateClosed:
{
NSLog(#"Session closed");
[FBSession.activeSession closeAndClearTokenInformation];
}
case FBSessionStateClosedLoginFailed:
NSLog(#"Login failed");
[self.navController popToRootViewControllerAnimated:NO];
[FBSession.activeSession closeAndClearTokenInformation];
[self showLoginView];
break;
default:
break;
}
if (error) {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:#"Error"
message:error.localizedDescription
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
}

I am not sure the Facebook SDK supports that - although I might be wrong. The idea (as I understand it) is that Facebook login allows the currently logged in Facebook user to login to your app. If no one is logged in to Facebook, the credentials page will be shown. After providing valid credentials, the user will be logged in to Facebook and an auth_token is sent to your app.
The point is that you can't authenticate a different user than the one currently logged in to Facebook. That would first require a logout of the current user and I think Facebook wish to keep this "change user" functionality to the Facebook app.
Also, note that when logging out from your app, the user stays logged in to Facebook. This is different from the policy that applies to web sites that use Facebook authentication. In that case, the user should be logged out both from the site and from Facebook.

Related

App rejection by apple on facebook login

I am using login with facebook in my app and getting username and profile pic from facebook. I have tested and it is working fine at my end, but apple has rejected it two times. I dont find any error at my end .
This is error by apple team:-
" We still found that your app exhibited one or more bugs, when reviewed on iPad Air running iOS 7.1 and an iPhone 5s running iOS 7.1, on both Wi-Fi and cellular networks, which is not in compliance with the App Store Review Guidelines.
Specifically, when we tap login to Facebook we get message say it wants to connect and when we tap ok it does not advance and connect with Facebook. "
Here is my Code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window = window;
self.login=[[ViewController alloc]init];
_nav = [[UINavigationController alloc] initWithRootViewController:self.login];
self.window.rootViewController = _nav;
[self.window makeKeyAndVisible];
// Override point for customization after application launch.
// Whenever a person opens the app, check for a cached session
if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded) {
NSLog(#"Found a cached session");
// If there's one, just open the session silently, without showing the user the login UI
[FBSession openActiveSessionWithReadPermissions:#[#"basic_info", #"email", #"user_likes"]
allowLoginUI:NO
completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
// Handler for session state changes
// This method will be called EACH time the session state changes,
// also for intermediate states and NOT just when the session open
[self sessionStateChanged:session state:state error: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
NSUserDefaults *storeData=[NSUserDefaults standardUserDefaults];
[storeData setObject:user.id forKey:#"user_id"];
[storeData setObject:user.name forKey:#"name"];
}
}];
break;
case FBSessionStateClosed:
case FBSessionStateClosedLoginFailed:
[FBSession.activeSession closeAndClearTokenInformation];
break;
default:
break;
}
}];
// If there's no cached session, we will show a login button
} else {
//UIButton *loginButton = [self.login loginButton];
//[loginButton setTitle:#"Log in with Facebook" forState:UIControlStateNormal];
}
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
// This method will handle ALL the session state changes in the app
- (void)sessionStateChanged:(FBSession *)session state:(FBSessionState) state error:(NSError *)error
{
// If the session was opened successfully
if (!error && state == FBSessionStateOpen){
NSLog(#"Session opened");
// Show the user the logged-in UI
[self userLoggedIn];
return;
}
if (state == FBSessionStateClosed || state == FBSessionStateClosedLoginFailed){
// If the session is closed
NSLog(#"Session closed");
// Show the user the logged-out UI
[self userLoggedOut];
}
// Handle errors
if (error){
NSLog(#"Error");
NSString *alertText;
NSString *alertTitle;
// If the error requires people using an app to make an action outside of the app in order to recover
if ([FBErrorUtility shouldNotifyUserForError:error] == YES){
alertTitle = #"Something went wrong";
alertText = [FBErrorUtility userMessageForError:error];
[self showMessage:alertText withTitle:alertTitle];
} else {
// If the user cancelled login, do nothing
if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryUserCancelled) {
NSLog(#"User cancelled login");
// Handle session closures that happen outside of the app
} else if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryAuthenticationReopenSession){
alertTitle = #"Session Error";
alertText = #"Your current session is no longer valid. Please log in again.";
[self showMessage:alertText withTitle:alertTitle];
// For simplicity, here we just show a generic message for all other errors
// You can learn how to handle other errors using our guide: https://developers.facebook.com/docs/ios/errors
} else {
//Get more error information from the error
NSDictionary *errorInformation = [[[error.userInfo objectForKey:#"com.facebook.sdk:ParsedJSONResponseKey"] objectForKey:#"body"] objectForKey:#"error"];
// Show the user an error message
alertTitle = #"Something went wrong";
alertText = [NSString stringWithFormat:#"Please retry. \n\n If the problem persists contact us and mention this error code: %#", [errorInformation objectForKey:#"message"]];
[self showMessage:alertText withTitle:alertTitle];
}
}
// Clear this token
[FBSession.activeSession closeAndClearTokenInformation];
// Show the user the logged-out UI
[self userLoggedOut];
}
}
// Show the user the logged-out UI
- (void)userLoggedOut
{
// Set the button title as "Log in with Facebook"
// UIButton *loginButton = [self.login loginButton];
//[loginButton setTitle:#"Log in with Facebook" forState:UIControlStateNormal];
// Confirm logout message
//[self showMessage:#"You're now logged out" withTitle:#""];
}
// Show the user the logged-in UI
- (void)userLoggedIn
{
// Set the button title as "Log out"
// UIButton *loginButton = self.login.loginButton;
//[loginButton setTitle:#"Log out" forState:UIControlStateNormal];
FrontViewController *v=[[FrontViewController alloc]init];
RearViewController *rearViewController = [[RearViewController alloc] init];
UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:v];
UINavigationController *rearNavigationController = [[UINavigationController alloc] initWithRootViewController:rearViewController];
SWRevealViewController *revealController = [[SWRevealViewController alloc] initWithRearViewController:rearNavigationController frontViewController:frontNavigationController];
revealController.delegate = self;
[self.nav pushViewController:revealController animated:YES];
[self showMessage:#"You're now logged in" withTitle:#"Welcome!"];
}
// Show an alert message
- (void)showMessage:(NSString *)text withTitle:(NSString *)title
{
[[[UIAlertView alloc] initWithTitle:title
message:text
delegate:self
cancelButtonTitle:#"OK!"
otherButtonTitles:nil] show];
}
// During the Facebook login flow, your app passes control to the Facebook iOS app or Facebook in a mobile browser.
// After authentication, your app will be called back with the session information.
// Override application:openURL:sourceApplication:annotation to call the FBsession object that handles the incoming URL
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation
{
return [FBSession.activeSession handleOpenURL:url];
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Handle the user leaving the app while the Facebook login dialog is being shown
// For example: when the user presses the iOS "home" button while the login dialog is active
[FBAppCall handleDidBecomeActive];
}
In My View Controller
- (IBAction)buttonTouched:(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 {
// Open a session showing the user the login UI
// You must ALWAYS ask for basic_info permissions when opening a session
[FBSession openActiveSessionWithReadPermissions:#[#"basic_info", #"email", #"user_likes"]
allowLoginUI:YES
completionHandler:
^(FBSession *session, FBSessionState state, NSError *error) {
// Retrieve the app delegate
AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
// Call the app delegate's sessionStateChanged:state:error method to handle session state changes
[appDelegate sessionStateChanged:session state:state error: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);
NSUserDefaults *storeData=[NSUserDefaults standardUserDefaults];
[storeData setObject:user.id forKey:#"user_id"];
[storeData setObject:user.name forKey:#"name"];
}
}];
break;
case FBSessionStateClosed:
case FBSessionStateClosedLoginFailed:
[FBSession.activeSession closeAndClearTokenInformation];
break;
default:
break;
}
}];
}
}
Is your app on developer.facebook.com public and available for all users?
Status should look like this:
In other case if your app has status Developer Mode then other users can't authorise by Facebook.
I have exactly the same issue. Although it's a Flutter app, but the use-case is the same.
On my end, everything works as expected
Fb app has iOS and Bundle ID set correctly
FB app is public
But the Apple review team still rejects it. I asked for more Screenshots, which they provided, AND the screenshots clearly show the "continue" dialog of the FB-popup. So I don't understand why they rejected it.

Facebook SDK iOS access code issue

My Project is to get Facebook Insights data of my Fanpage on my iPhone App.
I started by installing Facebook SDK for iOS to get Access Code & than using SampleLoginSample Sample Program provided with Facebook iOS SDK.
Its working Perfect & giving same response as in Facebook API Explporer for Query
[self.textNoteOrLink setText:
[NSString stringWithFormat:#"https://graph.facebook.com/[PAGEID]?fields=likes&access_token=%#",appDelegate.session.accessTokenData.accessToken]];
but if i am writing below Query its not giving me response as in Facebook Explorer API
QUERY:
[self.textNoteOrLink setText:
[NSString stringWithFormat:#"https://graph.facebook.com/PAGEID/insights/page_fans?access_token=%#",appDelegate.session.accessTokenData.accessToken]];
RESPONSE:
{"data":[
],"paging":{
"previous":"https:\/\/graph.facebook.com\/PAGEID\/insights\/page_fans?access_token=CAA...snip...h2l&since=1378984583&until=1379243783","next":"https:\/\/graph.facebook.com\/PAGEID\/insights\/page_fans?access_token=CAA...snip...h2l&since=1379502983&until=1379762183"}}
Can anyone help me to get access code with get_insights permission in Facebook iOS SDK?
In SLAppDelegate.h
Under the list of #property, add:
-(BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI;
In SLAppDelegate.m
Under applicationDidBecomeActive, add:
/*
* Opens a Facebook session and optionally shows the login UX.
*/
- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI {
NSArray *permissions = [[NSArray alloc] initWithObjects:
#"read_insights",
nil];
return [FBSession openActiveSessionWithReadPermissions:permissions
allowLoginUI:allowLoginUI
completionHandler:^(FBSession *session,
FBSessionState state,
NSError *error) {
[self sessionStateChanged:session
state:state
error:error];
}];
}
And:
/*
* Callback for session changes.
*/
- (void)sessionStateChanged:(FBSession *)session
state:(FBSessionState) state
error:(NSError *)error
{
switch (state) {
case FBSessionStateOpen:
if (!error) {
// We have a valid session
NSLog(#"User session found");
}
break;
case FBSessionStateClosed:
case FBSessionStateClosedLoginFailed:
[FBSession.activeSession closeAndClearTokenInformation];
break;
default:
break;
}
if (error) {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:#"Error"
message:error.localizedDescription
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
}
In SLViewController.h
At the end of viewDidLoad , add:
[appDelegate openSessionWithAllowLoginUI:NO];

iOS Facebook login doesn't work

I have an app, for iOS5 and above, in which I am trying to set up Facebook login. All works fine when there is a Facebook account integrated in the phone. If it is not, it doesn't work. This is even if the Facebook app is installed.
When the app is installed, it opens the app, asks me permission to access the appropriate user data and returns to my app with no result. It the same when it isn't installed. Safari is opened, asks me for authorization, returns, and nothing.
I followed all the steps mentioned in the Facebook Login tutorial. Is this a problem only I am facing?
EDIT: This is the requested code:
I open a session like so:
- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI {
NSArray *permissions = [[NSArray alloc] initWithObjects:
#"email",
nil];
return [FBSession openActiveSessionWithReadPermissions:permissions
allowLoginUI:allowLoginUI
completionHandler:^(FBSession *session,
FBSessionState state,
NSError *error) {
[self sessionStateChanged:session
state:state
error:error];
}];
}
And when it returns, I guess this method must be called. but it isn't:
- (void)sessionStateChanged:(FBSession *)session
state:(FBSessionState) state
error:(NSError *)error
{
switch (state) {
case FBSessionStateOpen:
if (!error) {
// We have a valid session
NSLog(#"session opened");
FBRequest *me = [FBRequest requestForMe];
__block NSString *username = nil;
[me startWithCompletionHandler:^(FBRequestConnection *connection,
id result,
NSError *error) {
NSDictionary<FBGraphUser> *my = (NSDictionary<FBGraphUser> *) result;
// NSLog(#"User session found: %#", my.username);
username = my.username;
}];
// /fb_access/:access_token
}
break;
case FBSessionStateClosed:
if (!error) {
// We have a valid session
NSLog(#"User session closed");
}
case FBSessionStateClosedLoginFailed:
NSLog(#"login failed, %#", error);
[FBSession.activeSession closeAndClearTokenInformation];
break;
default:
break;
}
[[NSNotificationCenter defaultCenter]
postNotificationName:FBSessionStateChangedNotification
object:session];
if (error) {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:#"Error"
message:error.localizedDescription
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
}
Any ideas?
add below key-value pair in your plist.
Add Field (key) FacebookAppID - (value) fbID
Add Field - URL Types (key)
Add 2 Field in URL Types:
(key) URL Identifier - (value) (i.e. YourApplication name)
(key)-URL Schemes
Add Field in URL Schemes (key) - (value) fb(fbID) (e.g. fb557354324264278)
Add following method in to your appdelgate
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{ // attempt to extract a token from the url return
[FBSession.activeSession handleOpenURL:url];
}
Both answers by iUser and Kalpesh are right. Combining both of them worked for me. Please refer to them. :)

Facebook Login You have already authorised APP_NAME

I have problem with facebook Login..I integrated Facebook into my app.First time when user wants to login it is showing login page,but from next time it is going to page "You have already authorised APP_NAME".
I checked for Scrumptious tutorial which is the sample which I got from Facebook SDK.and it is also working like same.
I want to login from different user,I dont want that page "You have already authorised APP_NAME" every time.
How to solve this.
Try this code in your app delegate class.
It checks whether the session is open or not and clears your login credentials from the device cache when you close the connection.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
LoginVC *loginScreen=[[LoginVC alloc]initWithNibName:#"LoginVC" bundle:nil];
navigationController=[[UINavigationController alloc]initWithRootViewController:loginScreen];
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
self.window.rootViewController=self.navigationController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
// See if we have a valid token for the current state.
if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded) {
[self openSession];
// To-do, show logged in view
} else {
// No, display the login page.
[self showLoginView];
}
return YES;
}
- (void)sessionStateChanged:(FBSession *)session
state:(FBSessionState) state
error:(NSError *)error
{
switch (state) {
case FBSessionStateOpen: {
UIViewController *topViewController =
[self.navigationController topViewController];
if ([[topViewController presentedViewController]
isKindOfClass:[LoginVC class]]) {
[topViewController dismissViewControllerAnimated:YES completion:nil];
}
}
break;
case FBSessionStateClosed:
case FBSessionStateClosedLoginFailed:
// Once the user has logged in, we want them to
// be looking at the root view.
[self.navigationController popToRootViewControllerAnimated:NO];
[FBSession.activeSession closeAndClearTokenInformation];
[self showLoginView];
break;
default:
break;
}
if (error) {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:#"Error"
message:error.localizedDescription
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
}
- (void)openSession
{
[FBSession openActiveSessionWithReadPermissions:nil
allowLoginUI:YES
completionHandler:
^(FBSession *session,
FBSessionState state, NSError *error) {
[self sessionStateChanged:session state:state error:error];
}];
}
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation
{
return [FBSession.activeSession handleOpenURL:url];
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
// We need to properly handle activation of the application with regards to Facebook Login
// (e.g., returning from iOS 6.0 Login Dialog or from fast app switching).
[FBSession.activeSession handleDidBecomeActive];
}
I hope this code will be helpful.

Parse.com and Facebook login, runs infinite loop

I updated both Parse and Facebook iOS SDKs to the latest versions, and when I try to login using Facebook my app crashes, and from the debugger I can see that it is calling 3-4 methods in an endless loop.
My login code looks like this:
- (void)openSession
{
UIViewController *topViewController = self.window.rootViewController;
NSArray *permissions = [NSArray arrayWithObjects:#"user_likes", #"friends_likes", nil];
// Login PFUser using Facebook
[PFFacebookUtils logInWithPermissions:permissions block:^(PFUser *user, NSError *error) {
if (!user) {
if (!error) {
NSLog(#"Uh oh. The user cancelled the Facebook login.");
} else {
NSLog(#"Uh oh. An error occurred: %#", error);
}
} else if (user.isNew) {
NSLog(#"User with facebook signed up and logged in!");
if ([[topViewController presentedViewController] isKindOfClass:[PALoginViewController class]]) {
[topViewController dismissViewControllerAnimated:YES completion:nil];
}
} else {
NSLog(#"User with facebook logged in!");
if ([[topViewController presentedViewController] isKindOfClass:[PALoginViewController class]]) {
[topViewController dismissViewControllerAnimated:YES completion:nil];
}
}
}];
}
The error occurs when logInWithPermissions is called. It crashes into Xcode and Debugger is showing an awful lot of calls to FB login methods - thousands actually:
What could be wrong?
This issue was reported as a bug on Facebook: https://developers.facebook.com/bugs/188127071335876?browse=search_5176d24c698df3761093726
It has now been fixed, and I can confirm that logging in now works - even with Sandbox enabled.
Sandbox off, then it should works.

Resources