10

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?

3
  • 1
    can't you use the WebView instead of launching Safari? Jan 24, 2011 at 18:27
  • sure, but even that looks amateur. i'd ideally want just just email and password fields
    – cannyboy
    Jan 24, 2011 at 18:59
  • 1
    Just a note from anyone who comes across this question in the future that a Web view is the expected method of authentication if you use the SDK in the simulator or on a phone without the Facebook app installed. If the Facebook app is available, the flow is much more seamless. But Facebook certainly doesn't want the email and password associated with the account to be kept "within the app." (That is, within 3rd party apps.) Sep 14, 2012 at 21:22

3 Answers 3

9

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];
}
2
  • i'll try using nsuserdefaults. as for using uiwebview, it seems a bit clumsy. i might try having email and password uitextfields and injecting these with javascript into an invisible uiwebview, and clicking the confirm button with javascript too
    – cannyboy
    Jan 24, 2011 at 19:05
  • 1
    I added some more sample code. As for it appearing clumsy.. though I agree with you that Facebook's implementation looks better and less "clumsy", I'd say that using the UIWebView is perfectly acceptable and (now) expected for Facebook login. IMHO, I wouldn't worry too much about it...
    – donkim
    Jan 24, 2011 at 19:09
5

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?

3

When you first auth, make sure you're asking for "offline_access" permission. That will make the token that OAuth returns to you NOT be invalidated at the end of the session, but instead stay valid literally until they come along and use the API to log your app OUT of Facebook.

Then, obviously, you need to save the token (I feel like NSUserDefaults is a fine place for it) and reuse it on later FB interactions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.