0

I am working on an iOS app using the latest FB SDK for native log in. When I switch my app off in "allow these apps to use your account" in the settings, an error "com.facebook.sdk error 2" is expected to come.

I am wondering is there any elegant way to solve this error even if "allow these apps to use your account" is off for my app? I have searched for the solution but all the answers are saying that You need to switch that option on. But I think the better way is that if user switches that option off, we can still let him log in, falling back to the fast-app-switch way seamlessly, just like he doesn't log into Facebook on his device at all. How can I do this in the newest FB SDK? Thanks!

====================================Update========================================= I solve it using a deprecated function openActiveSessionWithPermissions:allowLoginUI:completionHandler

first we need to check whether user switch this option off:

    self.useAccountAllowed = true;
    ACAccountStore *accountStore;
    ACAccountType *accountTypeFB;
    if ((accountStore = [[ACAccountStore alloc] init]) &&
        (accountTypeFB = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook] ) ){

        NSArray *fbAccounts = [accountStore accountsWithAccountType:accountTypeFB];
        id account;
        if (!fbAccounts)
        {
            //do not log into FB on the device
        }
        else if ([fbAccounts count] == 0) {
            [FBSession.activeSession closeAndClearTokenInformation];
            self.useAccountAllowed = false;  //user switch this option off
        } 

then in openSession function, using that deprecated function if self.useAccountAllowed is false:

if (self.useAccountAllowed) {
        [FBSession openActiveSessionWithReadPermissions:nil allowLoginUI:YES completionHandler:^(FBSession* session, FBSessionState status, NSError* error){
            [self sessionStateChanged:session state:status error:error];}];
    }
    else {
        NSArray* lPermission = FBSession.activeSession.permissions;
        [FBSession openActiveSessionWithPermissions:lPermission allowLoginUI:YES completionHandler:^(FBSession* session, FBSessionState status, NSError* error){
            [self sessionStateChanged:session state:status error:error];}];

not sure whether it is a correct way.

cao lei
  • 891
  • 1
  • 9
  • 19

1 Answers1

0

This is how I solved it. On the AppDelegate implementation file, in the applicationDidBecomeActive method, use the regular [FBSession.activeSession handleDidBecomeActive] method, as recommended by the FB SDK documentation. Plus, add a new method that checks the user permissions in Settings (that I called checkPermissionSettings in the example below):

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    NSLog(@"applicationDidBecomeActive: in NHOCAppDelegate");
    //
    // The flow back to your app may be interrupted (for ex: if the user clicks the Home button
    // while if authenticating via the Facebook for iOS app).
    // If this happens, the Facebook SDK can take care of any cleanup that may include starting a fresh session.
    //
    [FBSession.activeSession handleDidBecomeActive];
    [self checkPermissionSettings];
}

//
// Verify if the user pressed the Home Button, went to Settings and deauthorized the app via "Allow These Apps to Use Your Account..."
// If so, redirect him to the login screen (this happens automagically, see below).
//
- (void)checkPermissionSettings
{
    NSLog(@"checkPermissionSettings: in NHOCAppDelegate");
    //
    // Now 'startForMeWithCompletionHandler' may return 'FBSessionStateClosed' (meaning that the user probably unauthorized the app in Settings).
    //
    // If that is the case:
    //
    //  - Hide the 'logged' View Controller
    //  - Remove it (NHOCLoggedVC) from the Notification Center
    //  - Show the 'login' View Controller
    //  - And finally add it (NHOCLoginVC) to the Notification Center, closing the loop
    //
    // Check the console for further info.
    //
    [FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, id<FBGraphUser> user, NSError *error) {

        if (!error) {
            //
            // Everything went fine... The app is in good shape.
            // Notice that 'user.location' requires user_location permission
            //
            NSLog(@"user.location: %@: ", [user.location objectForKey:@"name"]);
        }
    }];
}

To make it work as designed, I also use Notification Center. You can check the entire example here:

FB SDK + Storyboards with Publish to Feed

I hope it helps.

backslash-f
  • 7,923
  • 7
  • 52
  • 80
  • Thanks. I will try whether it works. I am using another way with a deprecated function openActiveSessionWithPermissions:allowLoginUI:completionHandler: – cao lei Apr 17 '13 at 20:21
  • Coolio! My first attempt was also based on the function that you mentioned. It works, but like you said, it's deprecated. O.o See ya. – backslash-f Apr 17 '13 at 21:24