iOS: user's full name for an ACAccountTypeIdentifierFacebook

孤者浪人 提交于 2020-01-23 17:50:08

问题


I'm using the Facebook integration of iOS to allow user to sign in with their Facebook account.

I encountered a problem getting the user's full name.

I'm creating an ACAccount of type ACAccountTypeIdentifierFacebook.

After some searching I found out this little snippet to get the full name:

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

I tested it, and it worked. It worked on several devices.

Then we've send it to our client, he installed it, and it didn't work.

After a few days of testing, I was able to get the error happening on an iPhone from a co-worker.

After a quick debug session, I found out that the fullname key wasn't present. Instead there were two other keys, ACPropertyFullName and ACUIAccountSimpleDisplayName.

Now my code to get the full name is:

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

So my question is actually divided in three parts:

  1. Is it possible for the same thing to happen with the uid key, and If so, what possible keys exist?

  2. Is there any other keys to get the full name?

  3. Does the same thing happens on Twitter, or it always uses the same keys?

Thanks y'all.


回答1:


What you're doing there with the valueForKey:@"properties" call is accessing a private property and it will get your app rejected by Apple.

If your project is an iOS 7 project, you can use a new property on the ACAccount class called userFullName. From 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);

Alternatively, you can use the Graph API to query the current user using the Social framework:

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);
        }
    }
}];


来源:https://stackoverflow.com/questions/19714824/ios-users-full-name-for-an-acaccounttypeidentifierfacebook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!