Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do you open a FBSession with a completion handler that does not get retained and called on every session state change?
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>Here is a the method that is being used to login to Facebook:</p> <pre><code>- (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&lt;FBGraphUser&gt; 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]); } } </code></pre> <p>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.</p> <p>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.</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload