10

I'm attempting to get an account from ACAccountStore using the following code:

- (void) facebookLoginOnSuccess: (void (^)(void)) successBlock onError:(void(^)(NSError *error))errorBlock{

    self.facebookPermissions = @[
        @"offline_access",
        @"publish_stream",
        @"user_birthday",
        @"user_location",
        @"email"
    ];
    
        
    NSDictionary *options = @{
        @"ACFacebookAppIDKey": [[NSBundle mainBundle] objectForInfoDictionaryKey:@"FacebookAppID"],
        @"ACFacebookAppVersionKey": @"1.0",
        @"ACFacebookPermissionsKey": self.facebookPermissions,
        @"ACFacebookPermissionGroupKey": @"write"
    };
    
    [self accountLoginFor:ACAccountTypeIdentifierFacebook withOptions:options OnSuccess:successBlock onError:errorBlock];

}

- (void) accountLoginFor: (NSString *) accountTypeID withOptions: (NSDictionary *) options OnSuccess: (void (^)(void)) successBlock onError:(void(^)(NSError *error))errorBlock{

    ACAccountStore *accountStore = [[ACAccountStore alloc] init];
    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:accountTypeID];
    
    [accountStore requestAccessToAccountsWithType:accountType
                                          options:options
                                       completion:^(BOOL granted, NSError *error){
        if (granted){
            NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
            NSLog(@"%@",accountsArray);
        }
        else {
            NSLog(@"Error accessing account: %@", [error localizedDescription]);
        }
    }];
    
}

But I'm getting this error:

Error Domain=com.apple.accounts Code=6 "The operation couldn't be completed. (com.apple.accounts error 6.)"

And I can't find anything related, just this question. Any ideas what could be wrong?

Update

I found this on the Apple Developer Docs.

Accounts Framework

When requesting access to Facebook accounts, the only key required in your options dictionary is ACFacebookAppIdKey. ACFacebookPermissionGroupKey and ACFacebookAppVersionKey are now obsolete.

If you request a write permission under ACFacebookPermissionsKey, such as publish_stream, you must provide a value for ACFacebookAudienceKey, which can be one of ACFacebookAudienceEveryone, ACFacebookAudienceFriends, or ACFacebookAudienceOnlyMe.

So I changed my options to:

    NSDictionary *options = @{
        @"ACFacebookAppIDKey": [[NSBundle mainBundle] objectForInfoDictionaryKey:@"FacebookAppID"],
        @"ACFacebookPermissionsKey": self.facebookPermissions,
        @"ACFacebookAudienceKey": ACFacebookAudienceFriends
    };

But I'm getting the same error.

Community
  • 1
  • 1
clopez
  • 4,372
  • 3
  • 28
  • 42
  • I'm pretty sure it doesn't matter, but those keys are strings already: `NSDictionary *options = @{ACFacebookAppIDKey:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"FacebookAppID"], ACFacebookPermissionsKey:self.facebookPermissions, ACFacebookAudienceKey:ACFacebookAudienceFriends};` – jab Sep 27 '12 at 22:18
  • I don't understand your comment. I'm using the new syntax for defining Dictionaries/Arrays on XCode 4.5 :) – clopez Sep 28 '12 at 14:05
  • what jab was saying is that you don't need @"ACFacebookAppID", but can just write ACFacebookAppID. So you can drop the @"" ... however, I agree with him that it shouldn't matter. Try anyway. – Kristof Van Landschoot Oct 04 '12 at 09:53

3 Answers3

34

Ok so if you haven't setup an account from your Settings in iOS 6 it throws error code 6. If the user simply denies permission than it throws error code 7. In case 6 i suggest you ask the user to first setup her account in Settings.

NSDictionary *options = @{
ACFacebookAppIdKey: @"1234567890",
ACFacebookPermissionsKey: @[@"publish_stream"],
ACFacebookAudienceKey: ACFacebookAudienceFriends
};

[self.accountStore requestAccessToAccountsWithType:facebookAccountType options:options completion:^(BOOL granted, NSError *error)
{
    if (granted)
    {
        NSArray *accounts = [self.accountStore accountsWithAccountType:facebookAccountType];

        if([accounts count]>0)
            self.facebookAccount = [accounts lastObject];
    }
    else
    {
        dispatch_async(dispatch_get_main_queue(), ^{

            // Fail gracefully...
            NSLog(@"%@",error.description);
            if([error code]== ACErrorAccountNotFound)
                [self throwAlertWithTitle:@"Error" message:@"Account not found. Please setup your account in settings app."];
            else
                [self throwAlertWithTitle:@"Error" message:@"Account access denied."];

        });
    }
}];
brynbodayle
  • 6,546
  • 2
  • 33
  • 49
jAmi
  • 779
  • 7
  • 18
  • I can't test this right now, because I'm using FB SDK, but I think is a better answer than mine. – clopez Oct 16 '12 at 14:04
  • hello @jAmi , I have a problem regarding error , when I m displaying log then it executed perfectly but when I am alerting something using UIAlert then it quits , why? As u have done in if condition if([error code]==6) – Prince Kumar Sharma Dec 06 '12 at 10:31
  • 1
    @Khool please check you are not firing the UIAlertView from a background queue – jAmi Dec 09 '12 at 14:00
  • 4
    So instead of using `6`, please use `ACErrorAccountNotFound`. – Vincent Tourraine Jul 06 '13 at 17:43
  • remeber: in the first access you should request basic permission, only. after get this, you can request others permissions, like "read_stream" or "publish_stream" – seufagner Oct 18 '13 at 04:10
  • Is this still working code? I've read that public_stream is no more. Is there a workaround? I can't get it to work with publish_actions the supposed replacement for publish_stream. – user1904273 Sep 09 '15 at 11:32
4

jAmi has the right idea, but I don't like the idea of the magic numbers in the code. There is an enum that has the numbers built into it (ACErrorCode).

Cubs Fan Ron
  • 687
  • 6
  • 17
  • Thanks for pointing out the ACErrorCode enum. Much better using it than just specifying an integer – ribeto Apr 22 '13 at 16:35
1

My own solution was to use Facebook SDK instead of trying with the lib directly and now it's working.

clopez
  • 4,372
  • 3
  • 28
  • 42