【问题标题】:ACAccountStore Error 6 (ACErrorAccountNotFound) and 8ACAccountStore 错误 6 (ACErrorAccountNotFound) 和 8
【发布时间】:2014-03-29 03:39:29
【问题描述】:

我正在尝试使用以下代码从 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]);
        }
    }];
    
}

但是我收到了这个错误:

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

我找不到任何相关内容,只有这个question。有什么想法可能是错的吗?

更新

我在 Apple 开发者文档中找到了这个。

帐户框架

请求访问 Facebook 帐户时,选项字典中唯一需要的键是 ACFacebookAppIdKey。 ACFacebookPermissionGroupKey 和 ACFacebookAppVersionKey 现已过时。

如果您在 ACFacebookPermissionsKey 下请求写入权限,例如 publish_stream,您必须为 ACFacebookAudienceKey 提供一个值,该值可以是 ACFacebookAudienceEveryoneACFacebookAudienceFriends 或 ACFacebookAudienceOnlyMe

所以我将选项更改为:

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

但我遇到了同样的错误。

【问题讨论】:

  • 我很确定没关系,但这些键已经是字符串:NSDictionary *options = @{ACFacebookAppIDKey:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"FacebookAppID"], ACFacebookPermissionsKey:self.facebookPermissions, ACFacebookAudienceKey:ACFacebookAudienceFriends};
  • 我不明白你的评论。我正在使用新语法在 XCode 4.5 上定义字典/数组 :)
  • jab 说的是你不需要@"ACFacebookAppID",但可以写 ACFacebookAppID。因此,您可以删除 @"" ...但是,我同意他的观点,这无关紧要。还是试试吧。

标签: ios ios6


【解决方案1】:

好的,如果您尚未在 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."];

        });
    }
}];

【讨论】:

  • 我现在无法测试这个,因为我正在使用 FB SDK,但我认为这是一个比我更好的答案。
  • 你好@jAmi,我有一个关于错误的问题,当我显示日志时它执行得很好,但是当我使用 UIAlert 警告某些东西时它会退出,为什么?正如你在 if 条件中所做的那样 if([error code]==6)
  • @Khoo 请检查您没有从后台队列触发 UIAlertView
  • 所以不要使用6,请使用ACErrorAccountNotFound
  • 请记住:在第一次访问时,您应该请求基本权限。获取此信息后,您可以请求其他权限,例如“read_stream”或“publish_stream”
【解决方案2】:

jAmi 有正确的想法,但我不喜欢代码中的幻数的想法。有一个内置数字的枚举(ACErrorCode)。

【讨论】:

  • 感谢您指出 ACErrorCode 枚举。使用它比只指定一个整数要好得多
【解决方案3】:

我自己的解决方案是使用 Facebook SDK 而不是直接尝试使用 lib,现在它可以工作了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-11
    • 1970-01-01
    • 2013-05-01
    • 1970-01-01
    • 2013-10-06
    • 1970-01-01
    • 1970-01-01
    • 2013-03-12
    相关资源
    最近更新 更多