使用 FBSDKLoginkit 获取用户名和头像
Get User name and Profile picture using FBSDKLoginkit
我想使用 FBSDKLoginKit 获取用户名和个人资料图片。
如何得到它?
我的代码在这里。
pod 'FBSDKLoginKit', '~> 4.7'
- (IBAction)fbLoginActionClicked:(id)sender
{
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login
logInWithReadPermissions: @[@"public_profile"]
fromViewController:self
handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error) {
NSLog(@"Process error");
}
else if (result.isCancelled)
{
NSLog(@"Cancelled");
}
else
{
NSLog(@"Logged in");
}
}];
}
像下面的代码一样使用FBSDKGraphRequest
获取Facebook
用户的用户名和头像。
登录后,添加以下代码:
NSLog(@"Logged in");
if ([result.grantedPermissions containsObject:@"public_profile"]) {
if ([FBSDKAccessToken currentAccessToken]) {
NSMutableDictionary* parameters = [NSMutableDictionary dictionary];
[parameters setValue:@"name,picture.type(large)" forKey:@"fields"];
[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:parameters]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error)
{
NSLog(@"fetched user:%@", result);
NSString *name = [[result valueForKey:@"name"] lowercaseString];
NSString *username = [name stringByReplacingOccurrencesOfString:@" " withString:@""];
NSArray *picture_arr = [result objectForKey:@"picture"];
NSArray *data_arr = [picture_arr valueForKey:@"data"];
NSString *profile_pic = [data_arr valueForKey:@"url"];
}
}];
- (IBAction)fbLoginActionClicked:(id)sender
{
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login
logInWithReadPermissions: @[@"public_profile"]
fromViewController:self
handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error) {
NSLog(@"Process error");
}
else if (result.isCancelled)
{
NSLog(@"Cancelled");
}
else
{
NSLog(@"Logged in %@ %@", result.token.userID, result.token.tokenString);
[self fetchUserInfo];
}
}];
}
-(void)fetchUserInfo
{
if ([FBSDKAccessToken currentAccessToken])
{
NSLog(@"Token is available : %@",[[FBSDKAccessToken currentAccessToken]tokenString]);
[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"id, name, link, first_name, last_name, picture.type(large), email, birthday, bio ,location ,friends ,hometown , friendlists"}]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error)
{
NSLog(@"resultis:%@",result);
}
else
{
NSLog(@"Error %@",error);
}
}];
}
}
if ([FBSDKAccessToken currentAccessToken]) {
[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{ @"fields" : @"id,name,picture.width(100).height(100)"}]startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSString *nameOfLoginUser = [result valueForKey:@"name"];
NSString *imageStringOfLoginUser = [[[result valueForKey:@"picture"] valueForKey:@"data"] valueForKey:@"url"];
NSURL *url = [[NSURL alloc] initWithURL: imageStringOfLoginUser];
[self.imageView setImageWithURL:url placeholderImage: nil];
}
}];
}
我想使用 FBSDKLoginKit 获取用户名和个人资料图片。 如何得到它? 我的代码在这里。
pod 'FBSDKLoginKit', '~> 4.7'
- (IBAction)fbLoginActionClicked:(id)sender
{
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login
logInWithReadPermissions: @[@"public_profile"]
fromViewController:self
handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error) {
NSLog(@"Process error");
}
else if (result.isCancelled)
{
NSLog(@"Cancelled");
}
else
{
NSLog(@"Logged in");
}
}];
}
像下面的代码一样使用FBSDKGraphRequest
获取Facebook
用户的用户名和头像。
登录后,添加以下代码:
NSLog(@"Logged in");
if ([result.grantedPermissions containsObject:@"public_profile"]) {
if ([FBSDKAccessToken currentAccessToken]) {
NSMutableDictionary* parameters = [NSMutableDictionary dictionary];
[parameters setValue:@"name,picture.type(large)" forKey:@"fields"];
[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:parameters]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error)
{
NSLog(@"fetched user:%@", result);
NSString *name = [[result valueForKey:@"name"] lowercaseString];
NSString *username = [name stringByReplacingOccurrencesOfString:@" " withString:@""];
NSArray *picture_arr = [result objectForKey:@"picture"];
NSArray *data_arr = [picture_arr valueForKey:@"data"];
NSString *profile_pic = [data_arr valueForKey:@"url"];
}
}];
- (IBAction)fbLoginActionClicked:(id)sender
{
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login
logInWithReadPermissions: @[@"public_profile"]
fromViewController:self
handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error) {
NSLog(@"Process error");
}
else if (result.isCancelled)
{
NSLog(@"Cancelled");
}
else
{
NSLog(@"Logged in %@ %@", result.token.userID, result.token.tokenString);
[self fetchUserInfo];
}
}];
}
-(void)fetchUserInfo
{
if ([FBSDKAccessToken currentAccessToken])
{
NSLog(@"Token is available : %@",[[FBSDKAccessToken currentAccessToken]tokenString]);
[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"id, name, link, first_name, last_name, picture.type(large), email, birthday, bio ,location ,friends ,hometown , friendlists"}]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error)
{
NSLog(@"resultis:%@",result);
}
else
{
NSLog(@"Error %@",error);
}
}];
}
}
if ([FBSDKAccessToken currentAccessToken]) {
[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{ @"fields" : @"id,name,picture.width(100).height(100)"}]startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSString *nameOfLoginUser = [result valueForKey:@"name"];
NSString *imageStringOfLoginUser = [[[result valueForKey:@"picture"] valueForKey:@"data"] valueForKey:@"url"];
NSURL *url = [[NSURL alloc] initWithURL: imageStringOfLoginUser];
[self.imageView setImageWithURL:url placeholderImage: nil];
}
}];
}