Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone / iOS Facebook SDK - can you log in within the app, and keep the login credentials?

Is it possible to use the Facebook iOS SDK to authenticate within an app (not go to Safari), and also keep those authentication credentials for the next launch of the app?

When I try to use the demo app in the simulator, it always goes to safari to authenticate, which seems a bit crappy. And then, when I authenticate... if I completely kill the app it will ask to authenticate again (and then tell me I am already logged in)

Is there a way, to just present the user with just an email field and a password field and then keep that info.. within the app?

like image 398
cannyboy Avatar asked Jan 24 '11 18:01

cannyboy


People also ask

What is Facebook SDK for iOS?

The Facebook SDK enables: Facebook Login - Authenticate people with their Facebook credentials. Share and Send dialogs - Enable sharing content from your app to Facebook. App Events - Log events in your application.

How do I log into the Facebook app on my iPhone?

To start, go to the home screen on your iPhone (Image 1). Click on the dark blue icon with a white “f” on it to access your Facebook account (this is the Facebook application icon). Type in your, previously established, E-mail address and your Facebook password to log into your account (Image 2).

Why can't I log into the Facebook app on my iPhone?

Make sure that you have the latest version of the Facebook app, or delete the app and then reinstall it. Try logging in from a mobile browser (example: Safari, Chrome).


2 Answers

Take a look at this question and answer: Iphone facebook connect example calls safari. I don't want to use safari. Also, you'll want to store the authentication stuff in NSUserDefaults and check for them to make to prevent re-logins.

EDIT Some sample code:

To save login stuff:

[[NSUserDefaults standardUserDefaults] setObject:_facebook.accessToken forKey:@"AccessToken"];
[[NSUserDefaults standardUserDefaults] setObject:_facebook.expirationDate forKey:@"ExpirationDate"];
[[NSUserDefaults standardUserDefaults] synchronize];

To check for login stuff:

_facebook = [[[Facebook alloc] initWithAppId:@"[app_id]"] retain];
_facebook.accessToken    = [[NSUserDefaults standardUserDefaults] stringForKey:@"AccessToken"];
_facebook.expirationDate = (NSDate *) [[NSUserDefaults standardUserDefaults] objectForKey:@"ExpirationDate"];
if (![_facebook isSessionValid]) {
    [_facebook authorize:_permissions delegate:self];
}
else {
    [_facebook requestWithGraphPath:@"me" andDelegate:self];
}
like image 133
donkim Avatar answered Sep 21 '22 12:09

donkim


You can hack round it to stop it if this is what you really want, bearing in mind most other apps that migrate from the older Facebook connect api to graph will behave in the new way

In facebook.m find the following method

- (void)authorizeWithFBAppAuth:(BOOL)tryFBAppAuth
                    safariAuth:(BOOL)trySafariAuth

find the bottom of the didOpenOtherApp logic and comment out all above it so that it always opens inline and tuns this section of code thats contained in the !didOpenOtherApp braces

// If single sign-on failed, open an inline login dialog. This will require the user to
  // enter his or her credentials.
  if (!didOpenOtherApp) {
    [_loginDialog release];
    _loginDialog = [[FBLoginDialog alloc] initWithURL:loginDialogURL
                                          loginParams:params
                                             delegate:self];
    [_loginDialog show];
  }

However by doing this you are making it more likely that the user will have to input their credentials, which is surely worse than putting up with the fast app switching approach?

like image 40
kgutteridge Avatar answered Sep 24 '22 12:09

kgutteridge