10

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

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

更新

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

账户框架

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

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

所以我改变了我的选择:

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

但我遇到了同样的错误。

4

3 回答 3

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

        });
    }
}];
于 2012-10-16T10:31:44.367 回答
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).

于 2012-11-05T19:41:21.520 回答
1

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

于 2012-10-04T20:56:37.050 回答