简体   繁体   中英

How to login just in time to facebook in iOS app

Have reviewed the facbook pages on how to integrate facebook with iOS , but what i had been looking for is a bit different.

I would like to prompt for Facbook login only when a user decides to share stuff, the flow explained in FB docs walk thru how to login (handle asyc response from FB login) and show publish button, but what we need is to show "Post to FB" button, when the user clicks, i would like the user to login and then go to the preview of what is going to be posted and then post to FB.

I am using FB SDK and iOS 5, the difficulty is how to wire FB login flow directly to Post flow.

Thanks Elango

Below is some code that I wrote that does this. If the user has not signed in to Facebook from the device settings, it is a better user experience to just call openActiveSessionWithPublishPermissions: , which does both the read and publish permissions in one step. Otherwise, you just do the two permissions serially. As soon as the read permission succeeds, you do the publish permission.

I tested this code on an iOS6 and iOS5 device, using Facebook SDK 3.2.1

- (BOOL)hasFacebookInDeviceSettings
{
    ACAccountStore *accountStore = [[ACAccountStore alloc] init];
    ACAccountType *accountTypeFB = [accountStore accountTypeWithAccountTypeIdentifier:@"com.apple.facebook"];
    BOOL hasFacebookBuiltinAccount = (accountTypeFB != nil);
    return hasFacebookBuiltinAccount;
}

- (BOOL)hasLoggedInToFacebookInDeviceSettings
{
    if (![self hasFacebookInDeviceSettings]) {
        return NO;
    }
    BOOL result = [SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook];
    return result;
}

- (void)openFacebookSessionWithAllowLoginUI:(BOOL)allowLoginUI
{
    if (![self hasLoggedInToFacebookInDeviceSettings]) {
        // Simpler if we don't have the built in account
        [FBSession openActiveSessionWithPublishPermissions:@[@"publish_actions"]
                                           defaultAudience:FBSessionDefaultAudienceFriends
                                              allowLoginUI:allowLoginUI
                                         completionHandler:^(FBSession *session,
                                                             FBSessionState state,
                                                             NSError *error) {
                                             [self facebookSessionStateChanged:session
                                                                         state:state
                                                                         error:error];
                                         }];
    }
    else if (!FBSession.activeSession.isOpen) {
        __block BOOL recursion = NO;
        [FBSession openActiveSessionWithReadPermissions:nil
                                           allowLoginUI:allowLoginUI
                                      completionHandler:^(FBSession *session,
                                                          FBSessionState state,
                                                          NSError *error) {
                                          if (recursion) {
                                              return;
                                          }
                                          recursion = YES;
                                          if (error || !FBSession.activeSession.isOpen) {
                                              [self facebookSessionStateChanged:session
                                                                          state:state
                                                                          error:error];
                                          }
                                          else {
                                              assert(FBSession.activeSession.isOpen);
                                              if ([FBSession.activeSession.permissions indexOfObject:@"publish_actions"] == NSNotFound) {
                                                  [FBSession.activeSession requestNewPublishPermissions:@[@"publish_actions"]
                                                                                        defaultAudience:FBSessionDefaultAudienceFriends
                                                                                      completionHandler:^(FBSession *session,
                                                                                                          NSError *error) {
                                                                                          [self facebookSessionStateChanged:session
                                                                                                                      state:FBSession.activeSession.state
                                                                                                                      error:error];
                                                                                      }];
                                              }
                                          }
                                      }];
    }
}

hasFacebookInDeviceSettings tells you if this device even supports Facebook from the settings (ie this is iOS6+).

hasLoggedInToFacebookInDeviceSettings tells you if the user has signed into to Facebook from the iOS6 Facebook device settings.

You'll need to create your own facebookSessionStateChanged: and other code, as described in the login tutorial

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM