ios – ACAccountStore错误6(ACErrorAccountNotFound)和8

我试图使用以下代码从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 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.

所以我改变了我的选择:

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

但我收到同样的错误.

解决方法

好的,所以如果你没有在iOS 6中设置一个帐户,它将抛出错误代码6.如果用户只是拒绝许可而不是抛出错误代码7.如果我建议你请求用户首先设置她的帐户设置.
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."];

        });
    }
}];

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...