我试图使用以下代码从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上发现了这一点.
@H_301_14@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."]; }); } }];