我正在尝试使用本地(iOS 6-7x)库从我的应用程序授权用户访问Facebook。如果登录成功,我想将auth令牌传递给我的服务器。
以下代码在用户未在操作系统中设置其Facebook帐户时除外,工作正常。在这种情况下,我会收到以下错误:
Error Domain=com.apple.accounts Code=6 "The operation couldn’t be completed. (com.apple.accounts error 6.)
-(void) initFacebookLogin
{
LRAppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
if (appDelegate.accountStore == nil)
appDelegate.accountStore = [[ACAccountStore alloc] init];
__block ACAccount *facebookAccount = nil;
ACAccountType *facebookAccountType = [appDelegate.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSArray * permissions = @[@"publish_stream", @"publish_actions", @"email"];
NSMutableDictionary *options = [[NSMutableDictionary alloc] initWithObjectsAndKeys:FACEBOOK_APP_ID, ACFacebookAppIdKey, permissions, ACFacebookPermissionsKey, ACFacebookAudienceOnlyMe, ACFacebookAudienceKey, nil];
[appDelegate.accountStore requestAccessToAccountsWithType:facebookAccountType
options: options
completion: ^(BOOL granted, NSError *error) {
if ( granted )
{
NSArray *accounts = [appDelegate.accountStore accountsWithAccountType:facebookAccountType];
facebookAccount = [accounts lastObject];
ACAccountCredential* accountCredential = [facebookAccount credential];
NSString* token = [accountCredential oauthToken];
NSLog( @"token=%@", token );
}
else {
// WHAT DO I DO HERE???
// Error Domain=com.apple.accounts Code=6 "The operation couldn’t be completed. (com.apple.accounts error 6.)"
NSLog(@"%@", [error description]);
}
}];
}
我是否仍然需要使用Facebook SDK来要求用户登录? 是否有其他iOS原生库可以用来提示用户在iOS中设置Facebook访问权限?
或者,有没有更好的方法来进行Facebook认证(如果用户已经在操作系统中登录,则不要求用户再次登录)?