Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ACAccountType always showing 0 accounts

I have used facebook SDK for authentication with the facebook. When there is no facebook account enabled in settings then by click on the button apps goes to safari for authentication i.e.

if (appDelegate.session.state != FBSessionStateCreated)
    {
        appDelegate.session = [[FBSession alloc] init];
    }

    [appDelegate.session openWithCompletionHandler:^(FBSession *session,
                                                     FBSessionState status,
                                                     NSError *error) {
        // and here we make sure to update our UX according to the new session state
        [self updateView];
    }];

otherwise if facebook account is enabled then

    NSArray *fbAccounts=nil;
    ACAccountType *accountTypeFB;
    if ((_accountStore = [[ACAccountStore alloc] init]) &&
        (accountTypeFB = [_accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook] ) ){
        fbAccounts = [_accountStore accountsWithAccountType:accountTypeFB];
        NSLog(@" %d fbAccounts",[fbAccounts count]);
    }
    if ([fbAccounts count]!=0) {

        [FBSession openActiveSessionWithAllowLoginUI:YES];
        NSArray *permissions = [[NSArray alloc] initWithObjects:
                                @"email",
                                nil];

        [FBSession openActiveSessionWithReadPermissions:permissions allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
            if (error) {
                NSLog(@"Failure");
                NSLog(@"error %@",error);
            }
            else
            {
                NSLog(@" active session opened ");
                if (self.gotUserDetails)
                {
                    return;
                }
                [self fetchuserinfo];
            }
        }];
    }

enter image description here }

This image showing the alert to access user info. It is working fine in the simulator. But in device it always goes to safari or in facebook app for authentication even facebook account is enabled in the settings. Please help me out for finding this issue.

Thanks to all.

like image 746
Minkle Garg Avatar asked Nov 13 '22 03:11

Minkle Garg


1 Answers

You can't just call accountTypeWithAccountTypeIdentifier to access the FB account, you need to supply properties. This needs to be done using requestAccessToAccountsWithType:options:completion: and has an asynchronous response.

Move your backup web based auth into a different method, then, check if you can create the account store as you are. If not, call the web method. If you can, try to gain access to the FB account with requestAccessToAccountsWithType:options:completion:. In the completion block you will either have been granted access or you will call the web method.

This code should all run on the main thread. The asynchronous response may be called on a different thread so switch back to the main thread to complete your handling.


Fill in the ... parts.

- (void)webAuthorise {...}

- (void)authorise {
if(!_accountStore)
    _accountStore = [[ACAccountStore alloc] init];

ACAccountType *facebookTypeAccount = [_accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];

if (facebookTypeAccount == nil) {
    NSLog(@"Failed, No account");
    [self webAuthorise];
    return;
}

[_accountStore requestAccessToAccountsWithType:facebookTypeAccount
                                       options:@{ACFacebookAppIdKey: @"...", ACFacebookPermissionsKey: @[@"email"]}
                                    completion:^(BOOL granted, NSError *error) {
                                        dispatch_async(dispatch_get_main_queue(), ^{
                                        if(granted){
                                            NSArray *accounts = [_accountStore accountsWithAccountType:facebookTypeAccount];
                                            _facebookAccount = [accounts lastObject];
                                            NSLog(@"Success");

                                            [self ...];
                                        } else {
                                            NSLog(@"Failed, Error: %@", error);

                                            [self webAuthorise];
                                        }
                                        });
                                    }];
}
like image 108
Wain Avatar answered Nov 15 '22 06:11

Wain