2

Ive been searching for solution to what seems to be a simple problem for a while now but have had no luck. The login code is as follows.

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {

return [facebook handleOpenURL:url]; 
}

 - (void)applicationDidBecomeActive:(UIApplication *)application
{
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

    NSString *accessToken = [prefs stringForKey:@"facebook-accessToken"];
    NSDate *expirationDate = [prefs objectForKey:@"facebook-expirationDate"];
    facebook.accessToken = accessToken;
    facebook.expirationDate = expirationDate;
    if ([facebook isSessionValid]) {
    NSLog(@"valid session");
}
else {
    NSLog(@"NOT VALID");
    [facebook authorize:nil delegate:self];
}
}

- (void)fbDidLogin {
    NSLog(@"delegate method");
    NSString *accessToken = facebook.accessToken;
    NSDate *expirationDate = facebook.expirationDate;
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setObject:accessToken forKey:@"facebook-accessToken"];
    [prefs setObject:expirationDate forKey:@"facebook-expirationDate"];
    [prefs synchronize];
}

I have a property of Type Facebook in my rootViewController and a button to logout. I pass the facebook instance to the rootViewController and then try to log out as follows.

-(IBAction) logout {
    [facebook logout:self];

}

- (void)fbDidLogout { 
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setObject:nil forKey:@"facebook-accessToken"];
    [prefs setObject:nil forKey:@"facebook-expirationDate"];
    [prefs synchronize];
}

It kind of works..... Eg. When I do not log out I can restart the app even after closing it down properly (holding icon then pressing x) and the user will be logged in without having to do anything. But when I log out, the next time the app is started the user doesnt get a chance to re-enter the login details, instead I just get a facebook page saying "you have already authorised this app to use facebook"??

Does anyone know if I am going about this the wrong way as its really annoying me now?

Many thanks

Jules

Jules
  • 660
  • 8
  • 29
  • similar case here .. facebook logout from application/sdk successfully but its session still in safari plugin... if I again login then it shows that already login just only confirming the permission – Amit Battan Jun 03 '11 at 12:33

2 Answers2

2

The user might not need to re-enter his credentials again because of the single sign-on:

In the updated version of the SDK, we changed the authorization mechanism so that users no longer have to re-enter their credentials for every application on the device they want to authorize.

If the user is logged into the Facebook application, he will simply see the "you have already authorized this app to use Facebook" page.

The logout method clears all application state and makes a server request to invalidate the current access token.

Note that logging out will not revoke your application's permissions, but simply clears your application's access token. If a user that has previously logged out of your application returns, he will simply see a notification that he's logging into your application, not a notification to grant permissions.

albertamg
  • 28,492
  • 6
  • 64
  • 71
  • thanks for the respinse, however I have deleted all other apps that use FB and the issue is still present. I want the following behaviour if this helps... 1st time user logs in they are asked for login details. Any subsequent launches of the app should bypass the user login screen, UNLESS they have previously pressed the logout button. Is this even possible, Im sure it is as other apps do it, Im just not sure how? Many thanks again. – Jules May 11 '11 at 14:34
  • "I have deleted all other apps that use FB". Have you logged out from the Facebook app? Have you logged out of Facebook from Safari Mobile? Multiple applications can share the same Facebook user session through the Safari cookie. – albertamg May 11 '11 at 14:44
  • Ok I am testing on the simulator with no other apps. I logged out of facebook on safari and I am still in the same situation. Hmmmm. There must be someway of doing this as the facebook app itself does what I am trying to achieve, even if I am logged into FB on Safari? Many thanks again. – Jules May 11 '11 at 14:54
  • So in the simulator when you try to authorize your app, you say you are redirected to Safari straight to the "you have already authorized this app to use Facebook" page. At the bottom, it should say something like "Logged in as XXX. Not you?". "Not you" being a link that will log you out. – albertamg May 11 '11 at 15:07
  • Anyway, single sign-on is a feature, not an issue. Maybe you will have better luck if you ask a new question like "How to disable Facebook single sign on for iPhone?". I've found this other question: [How to disable Facebook single sign on for android](http://stackoverflow.com/questions/4521013/how-to-disable-facebook-single-sign-on-for-android-facebook-android-sdk) – albertamg May 11 '11 at 15:14
  • Ok I see your point as it being a feature. I just find it strange that even after logging out I am logged back in automatically... Thanks for your help. – Jules May 11 '11 at 15:16
  • This is because the `logout` method simply "clears all application state and makes a server request to invalidate the current access token." Therefore, "If a user that has previously logged out of your application returns, he will simply see a notification that he's logging into your application". The quoted parts are from the documentation. – albertamg May 11 '11 at 15:19
  • You can think of it as logging out of your application within FB, not from FB. – albertamg May 11 '11 at 15:26
  • I understand this. But there are fb apps out there such as Facebook for iPhone, myPad for iPad etc that have a logout button which requires the user to enter there credentials on the next app start up. They must be doing something different to SSO then? Thanks again – Jules May 11 '11 at 15:27
  • 4
    I don't know if there is a better way to go, so my recommendation is to ask a new question like "how to disable SSO for iPhone". Said that, I have read [in the comments of this post](http://developer.appcelerator.com/blog/2011/02/facebook-module-changes-in-titanium-mobile-1-6-0.html) you can get rid of SSO editing the source of Facebook.m, just changing one line in the implementation of the `authorize:delegate:` method by changing `[self authorizeWithFBAppAuth:YES safariAuth:YES];` to `[self authorizeWithFBAppAuth:NO safariAuth:NO];` – albertamg May 11 '11 at 15:48
  • Ha I literally just found the same thing and I tried it..... It seems to work. In fact it has the same behaviour of myPad which is good. Thanks for all your help. – Jules May 11 '11 at 16:12
-1

You need to verify the session before authorize so this code still works :

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

    NSString *accessToken = [prefs stringForKey:@"facebook-accessToken"];
    NSDate *expirationDate = [prefs objectForKey:@"facebook-expirationDate"];
    facebook.accessToken = accessToken;
    facebook.expirationDate = expirationDate;

if (![facebook isSessionValid]) {
    [facebook authorize:MY_APP_ID delegate:self];
}

}
TheRonin
  • 1,305
  • 1
  • 12
  • 18