iOS:用户ACAccountTypeIdentifierFacebook的全名

我正在使用iOS的Facebook集成,允许用户使用他们的Facebook帐户登录。

我在获取用户的全名时遇到了问题。

我正在创建一个类型为ACAccountACAccountTypeIdentifierFacebook

经过一番搜索,我发现这个小片段得到了全名:

 // account is an ACAccount instance NSDictionary *properties = [account valueForKey:@"properties"]; NSString *fullName = properties[@"fullname"]; 

我测试了它,它工作了。 它适用于多种设备。

然后我们将它发送给我们的客户,他安装了它,但它没有用。

经过几天的测试,我能够从同事那里得到iPhone上发生的错误。

在快速调试会话之后,我发现fullname键不存在。 相反,还有另外两个键, ACPropertyFullNameACUIAccountSimpleDisplayName

现在我获取全名的代码是:

 NSDictionary *properties = [account valueForKey:@"properties"]; NSString *nameOfUser = properties[@"fullname"]; if (!nameOfUser) { nameOfUser = properties[@"ACUIAccountSimpleDisplayName"]; if (!nameOfUser) { nameOfUser = properties[@"ACPropertyFullName"]; } } 

所以我的问题实际上分为三个部分:

  1. 使用uid键是否可能发生同样的事情,如果是这样,可能存在哪些键?

  2. 还有其他钥匙可以获得全名吗?

  3. 在Twitter上发生同样的事情,还是总是使用相同的密钥?

谢谢你们。

你使用valueForKey:@"properties"做什么valueForKey:@"properties"调用是访问私有财产,它会让你的应用被Apple拒绝。

如果您的项目是iOS 7项目,则可以在ACAccount类上使用名为userFullName的新属性。 来自ACAccount.h

 // For accounts that support it (currently only Facebook accounts), you can get the user's full name for display // purposes without having to talk to the network. @property (readonly, NS_NONATOMIC_IOSONLY) NSString *userFullName NS_AVAILABLE_IOS(7_0); 

或者,您可以使用Graph API使用Social框架查询当前用户 :

 SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodGET URL:[NSURL URLWithString:@"https://graph.facebook.com/me"] parameters:nil]; request.account = account; // This is the account from your code [request performRequestWithHandler:^(NSData *data, NSURLResponse *response, NSError *error) { if (error == nil && ((NSHTTPURLResponse *)response).statusCode == 200) { NSError *deserializationError; NSDictionary *userData = [NSJSONSerialization JSONObjectWithData:data options:0 error:&deserializationError]; if (userData != nil && deserializationError == nil) { NSString *fullName = userData[@"name"]; NSLog(@"%@", fullName); } } }];