6

For some strange reason Facebook has decided to create completion handlers that are actually just notification observers. Ignoring why they are doing this, I need some way to make a request to open a FBSession and have a callback happen only once because the objects inside the callback CANNOT be retained.

If the user presses a button and a Facebook session with "publish_actions" permissions is not open then an activity indicator should be displayed and prevent interaction with the UI while a Facebook session is opened.

Here is a the method that is being used to login to Facebook:

- (void)loginToFacebookFromViewController:(UIViewController *)viewController completion:(void (^)(NSString *facebookAccountName))completion
{
    if([self isLoggedIntoFacebook] == NO)
    {
        [viewController startLoadingAnimation];

        [FBSession openActiveSessionWithPublishPermissions: @[ @"publish_actions" ] defaultAudience:FBSessionDefaultAudienceFriends allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error)
            {
                if(error == nil)
                {
                    [FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, id<FBGraphUser> user, NSError *error)
                        {
                            NSString *accountName = [user name];

                            if(error == nil)
                            {
                                NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
                                [standardUserDefaults setObject:accountName forKey:TSFacebookAccountName];
                                [standardUserDefaults synchronize];
                            }

                            [viewController stopLoadingAnimation];

                            if(completion != nil)
                                completion(accountName);
                        }];
                }
                else
                {
                    [viewController stopLoadingAnimation];

                    if(completion != nil)
                        completion(nil);
                }
            }];
    }
    else
    {
        if(completion != nil)
            completion([self facebookAccountName]);
    }
}

If the completion handler was not retained (again for reasons that make no freaking sense because NSNotifications are posted when session states change) then this method would work perfectly.

Because the app does not revolve around Facebook I don't have any interest in when the session state changes. If the user attempts to share something to Facebook and the active session is not open the login process will be kicked off and if a session is opened the sharing process will continue. Otherwise, an error message will be shown to the user.

Could someone explain how Facebook expects this sort of functionality to be implemented because it seems like a pretty common use case and before someone mentions "why don't you use the default Facebook dialogs" it is because a custom UI needs to be presented to the user to gather specific information used for Open Graph posts.

2 Answers 2

7

If you do not care about receiving state changes from FBSessionStateHandler like I did then the the code below should allow you to login to Facebook and provide you with a completion block that is not retained.

The completion block that is passed into the method will always get used when any state change occurs and then immediately nil'd out so that if another state change occurs it is ignored.

typedef void(^FacebookLoginCompletionBlock)(id<FBGraphUser> user);

- (void)loginToFacebookFromWithCompletionBlock:(FacebookLoginCompletionBlock)completionBlock
{
    // If the user is not logged into Facebook attempt to open a session with "publish_actions" permissions.
    if([[FBSession activeSession] isOpen] == NO)
    {
        // Copy the completion block to a local variable so it can be nil'd out after it is used to prevent retain loops.
        __block FacebookLoginCompletionBlock copiedCompletionBlock = completionBlock;

        [FBSession openActiveSessionWithPublishPermissions:@[ @"publish_actions" ] defaultAudience:FBSessionDefaultAudienceFriends allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error)
            {
                // Only attempt to run any of this code if there is a completion block to call back to. If completion block is nil than it has already been used and this is a state change that we do not care about.
                if(copiedCompletionBlock != nil)
                {
                    // Because this method is only concerned with the user logging into Facebook just worry about the open state occuring with no errors.
                    if(status == FBSessionStateOpen && error == nil)
                    {
                        // If the user successfully logged into Facebook download their basic profile information so the app can save the information to display to the user what account they are logged in under.
                        [FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, id<FBGraphUser> user, NSError *error)
                            {
                                if(copiedCompletionBlock != nil)
                                    copiedCompletionBlock(user);

                                // nil out the copied completion block so it is not retained and called everytime the active FBSession's state changes.
                                copiedCompletionBlock = nil;
                            }];
                    }
                    // Because this method is only concerned with the user logging into Facebook if any other state is triggered call the completion block indicating that there was a failure.
                    else
                    {
                        if(copiedCompletionBlock != nil)
                            copiedCompletionBlock(nil);

                        // nil out the copied completion block so it is not retained and called everytime the active FBSession's state changes.
                        copiedCompletionBlock = nil;
                    }
                }

                // This block will exist the lifespan of the application because for some bizarre reason Facebook retains the completion handler for their open active session methods. Throw in some code that will display an error to the user if any session state changes occur that Facebook thinks the user should be aware of. Your code should be always checking if a active Facebook session exists before taking any action so not being aware of these changes should not be any issue. Worst case scenario you can listen for FBSessionDidSetActiveSessionNotification, FBSessionDidUnsetActiveSessionNotification, FBSessionDidBecomeOpenActiveSessionNotification or FBSessionDidBecomeClosedActiveSessionNotification notifications.
                if ([error fberrorShouldNotifyUser] == YES)
                {
                    NSString *alertTitle = @"Error logging into Facebook";
                    NSString *alertMessage = [error fberrorUserMessage];

                    if ([alertMessage length] == 0)
                        alertMessage = @"Please try again later.";

                    UIAlertView *alertView =  [[UIAlertView alloc] initWithTitle:alertTitle message:alertMessage delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

                    [alertView show];
                }
            }];
    }
    // If the user is already logged into Facebook immediately call the completion block with the user object that should have been saved when the user previously logged in.
    else
    {
        if(completionBlock != nil)
            completionBlock([self facebookUser]);
    }
}
1

The way I implemented similar behavior is to put my session logic in a singleton. The singleton implements a completion handler that can be safely called by FB whenever the session changes. My completion handler then posts its own notifications for other controllers to observe as necessary.

Let's assume you have a MySession singleton where you've defined some methods and notifications around FB auth. Your view controller logic would be something like:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // observe MySession notifications
    [MySession.sharedSession addObserver:self];
}

- (void)dealloc
{
    // stop observing MySession notifications
    [MySession.sharedSession removeObserver:self];
}

// this is a user action that requires FB auth
- (void)someActionRequiringFacebookAuth
{
    if (![MySession.sharedSession isLoggedIntoFacebook]) {
        self.postAuthAction = @selector(someActionRequiringFacebookAuth);
        [MySession.sharedSession loginToFacebook];
    }

    else {
        // perform action
    }
}

// notification posted by MySession before a FB auth attempt
- (void)mySessionWillAttemptFacebookAuth:(NSNotification *)notification
{
    [self startLoadingAnimation];
}

// notification posted by MySession if a FB auth attempt succeeds
- (void)mySessionDidAuthWithFacebook:(NSNotification *)notification
{
    [self stopLoadingAnimation];
    if (self.postAuthAction) {
        [self performSelector:self.postAuthAction];
        self.postAuthAction = nil;
    }
}

// notification posted by MySession if a FB auth attempt fails
- (void)mySessionFacebookAuthDidFail:(NSNotification *)notification
{
    [self stopLoadingAnimation];
    // display error if desired
    self.postAuthAction = nil;
}
1
  • I would prefer a solution that would not require multiple method declarations and add/remove observer calls. For simplicity I am looking for a way to have all my logic handled inside the someActionRequiringFacebookAuth method.
    – Reid Main
    May 24, 2013 at 19:13

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.