Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save Facebook account using ACAccountStore

I'm trying to use iOS Social.framework to share to Facebook. However, most users will not have Facebook setup in Settings>Facebook so therefore Facebook doesn't show up when you bring up a UIActivityViewController, it just isn't listed. (See UIActivity with no settings for Facebook or just google "facebook doesn't show in UIActivityViewController," there's plenty of people trying to solve this problem.)

Our app already uses the Facebook SDK so I can get at the Facebook account info, so I thought I would just get the info and then save the info in Settings>Facebook. Here's my code. (Which I got from When do we use saveAccount:(ACAccount *)account of ACAccountStore class? and iOS requestAccessToAccountsWithType is Not Showing Permission Prompt / NSAlert)

- (void)storeAccountWithAccessToken:(NSString *)token secret:(NSString *)secret {

    if (self.accountStore == nil) {
        self.accountStore = [[ACAccountStore alloc] init];
    }

    //We start creating an account by creating the credentials object
    ACAccountCredential *credential = [[ACAccountCredential alloc] initWithOAuthToken:token tokenSecret:secret];
    ACAccountType *acctType =[self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
    ACAccount *newAccount = [[ACAccount alloc] initWithAccountType:acctType];
    //Here we assign credentials and now can save account
    newAccount.credential = credential;

    NSDictionary *options = [[NSDictionary alloc] initWithObjectsAndKeys:
                             [VVEnvironment stringForKey:kVVEnvironmentKeyFacebookAppID], (NSString *)ACFacebookAppIdKey,
                             [NSArray arrayWithObject:@"basic_info"], (NSString *)ACFacebookPermissionsKey,
                             ACFacebookAudienceEveryone, (NSString *)ACFacebookAudienceKey,
                             nil];


    [_accountStore requestAccessToAccountsWithType:acctType options:options completion:^(BOOL granted, NSError *error) {
        if (granted == YES) {
            [self.accountStore saveAccount:newAccount withCompletionHandler:^(BOOL success, NSError *error) {

                if (success) {
                    NSLog(@"the account was saved!");
                }
                else {
                    if ([error code] == ACErrorPermissionDenied) {
                        NSLog(@"Got a ACErrorPermissionDenied, the account was not saved!");
                    }
                    NSLog(@"the account was not saved! %d, %@", [error code], error);
                }
            }];
        }
        else {
            NSLog(@"ERR: %@ ",error);
        }
    }];
}

I added the call to requestAccessToAccountsWithType there because without I was getting a ACErrorPermissionDenied back from saveAccount. When I run this I do see my app slide briefly aside for the Facebook dialog (if I delete my app on Facebook's website it does bring up the full dialog asking me for permissions which I grant.)

The problem is that granted is always NO, so I never get a chance to save the account. What am I missing here?

I am running this on a real device that has the Facebook App installed and setup.

I'd appreciate any help on this, thanks!

like image 724
Paul Cezanne Avatar asked Nov 11 '22 17:11

Paul Cezanne


1 Answers

There is no solution available for this. iOS native facebook applications are not allowed to write or save account to ACAccountStore. It only allow to access account from the ACAccountStore. iOS only allow to store 1 facebook account at a time. Try to do these steps you will understand this easily,

  1. Add a facebook account to settings App and try to get permission to access it. It will grand permission.

  2. Remove account and try to get the permission it will deny access.

  3. Add a facebook account and get access and try to save new account to ACAccountStore, It will say that Native applications are not allowed to save account to ACAccountStore.

The proposed solution is, Add the account to settings App and login with it and Share using Social framework or presentOSIntegratedDialog.

like image 168
Rafeek Avatar answered Nov 30 '22 19:11

Rafeek