Getting user's Personal info from Facebook in iOS

前端 未结 4 1034
悲哀的现实
悲哀的现实 2020-12-08 00:50

I am quite new to objective-C and iPhone Development environment.

I am implementing Facebook login in my app to get User\'s name, Email and profile Picture. I have s

4条回答
  •  独厮守ぢ
    2020-12-08 01:35

    To get user Email ID you must ask permission for email while logging.

    FBSDKLoginButton *loginView = [[FBSDKLoginButton alloc] init];
    loginView.readPermissions =  @[@"email"];
    loginView.frame = CGRectMake(100, 150, 100, 40);
    [self.view addSubview:loginView];
    

    You can get user email Id in New SDK using GraphPath.

    [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:nil]
             startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
    
                 if (!error) {
                     NSLog(@"fetched user:%@  and Email : %@", result,result[@"email"]);
             }
             }];
        }
    

    result would get you all the user Details and result[@"email"] would get you the email for logged in user.

    To get Profile picture you can use

    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=normal",result[@"id"]]];
    NSData  *data = [NSData dataWithContentsOfURL:url];
    _imageView.image = [UIImage imageWithData:data]; 
    

    or u can also use FBSDKProfilePictureView to get profile Picture by passing user profile Id:

    FBSDKProfilePictureView *profilePictureview = [[FBSDKProfilePictureView alloc]initWithFrame:_imageView.frame];
    [profilePictureview setProfileID:result[@"id"]];
    [self.view addSubview:profilePictureview];
    

    Refer to :https://developers.facebook.com/docs/facebook-login/ios/v2.3#profile_picture_view

    or u can also get both by passing as parameters

    [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me"
                                               parameters:@{@"fields": @"picture, email"}]
             startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
                 if (!error) {
                     NSString *pictureURL = [NSString stringWithFormat:@"%@",[result objectForKey:@"picture"]];
    
                     NSLog(@"email is %@", [result objectForKey:@"email"]);
    
                     NSData  *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:pictureURL]];
                     _imageView.image = [UIImage imageWithData:data];
    
                 }
                 else{
                     NSLog(@"%@", [error localizedDescription]);
                 }
             }];
    

提交回复
热议问题