ACAccountStore错误6(ACErrorAccountNotFound)和8

clo*_*pez 10 ios ios6

我正在尝试使用以下代码从ACAccountStore获取帐户:

- (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]);
        }
    }];

}
Run Code Online (Sandbox Code Playgroud)

但是我收到了这个错误:

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

我找不到任何相关的东西,只是这个问题.什么想法可能是错的?

更新

我在Apple Developer Docs上找到了这个.

账户框架

请求访问Facebook帐户时,选项词典中唯一需要的密钥是ACFacebookAppIdKey.ACFacebookPermissionGroupKey和ACFacebookAppVersionKey现已过时.

如果您在ACFacebookPermissionsKey下请求写入权限(例如publish_stream),则必须为ACFacebookAudienceKey提供值,该值可以是ACFacebookAudienceEveryone,ACFacebookAudienceFriends或ACFacebookAudienceOnlyMe之一.

所以我将选项更改为:

    NSDictionary *options = @{
        @"ACFacebookAppIDKey": [[NSBundle mainBundle] objectForInfoDictionaryKey:@"FacebookAppID"],
        @"ACFacebookPermissionsKey": self.facebookPermissions,
        @"ACFacebookAudienceKey": ACFacebookAudienceFriends
    };
Run Code Online (Sandbox Code Playgroud)

但是我得到了同样的错误.

jAm*_*Ami 34

好的,如果您还没有在iOS 6中的"设置"中设置帐户,则会抛出错误代码6.如果用户只是拒绝权限,则抛出错误代码7.在案例6中,我建议您要求用户首先在"设置"中设置她的帐户.

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."];

        });
    }
}];
Run Code Online (Sandbox Code Playgroud)

  • 因此,请使用`ACErrorAccountNotFound`而不是使用`6`. (4认同)