How to save facebook account to ACAccountStore from facebookSDK

1k views Asked by At

I want to save the facebook account to ACAccountStore in iOS,

  1. I will get accessTocken from facebookSDK but how to obtain tocken and secret
  2. How to save it to ACAccountStore

Thanks in Advance.

------ Edit--------

I have an application that uses facebook to login and after with login information need to share photos that he selected. My use cases are,

  1. If account already in settings app then I can use it for login and share.

  2. User using safari or webview for login then how I can proceed for sharing.

    a. facebook SDK says If the user need to use OS integrated share controller then, login method used to authenticate the user must be native iOS 6.0 authentication..

    b. If I need to share option that provided by facebook SDK uses facebook App I don't want this option because I want to avoid App switching as much as possible.

How to solve this..

1

There are 1 answers

6
Zeeshan On

to do it you should have an accesstoken and secret,

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

//make credentials to assign to ACAccount 
ACAccountCredential *credential = [[ACAccountCredential alloc] initWithOAuthToken:accesstoken tokenSecret:secret];
ACAccountType *acctType =[self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
ACAccount *newAccount = [[ACAccount alloc] initWithAccountType:acctType];

newAccount.credential = credential;

NSDictionary *options = [[NSDictionary alloc] initWithObjectsAndKeys:
                         "xXXXXXX", (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(@"%@",error);
            }
        }];
    }
    else {
        NSLog(@"%@ ",error);
    }
}];