How To Fetch All Data From Facebook Using FBLoginView iOS

photo from Speedoox
Hi, welcome again to my new tutorial. How to get all data from Facebook using LoginView - iOS Objective-C.

Thinking about the time when I was discovering how to fetch all the data using LoginView from Facebook to iOS makes me laugh. Why? Because I spent almost 3 hours solving my problem. I wasn't able to get even a Facebook Profile Picture. Ughhh... But now, I am sharing this to you guys. If you want to send me your gratitude, just post it in the comment section below of the post :) or subscribe or share.

I have removed some parts of my codes here because this is actually for my project in the office. NDA issue lol. Okay, so I am using FBLoginView. Now, there are lots of answers posted on stackoverflow, but most of them didn't work to me. Facebook provides many ways or rather many kinds of SDK to use on iOS. Let's get started.

- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id)user
{
    NSURL *smallResPicURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=small",user.id]];
    NSLog(@"%@", smallResPicURL);

    // Start Fetching Data...
    [FBRequestConnection startWithGraphPath:@"me" parameters:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"id,first_name,last_name,email,picture.width(720).height(720)",@"fields",nil] HTTPMethod:@"GET" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
        NSDictionary *userData = (NSDictionary *)result;
        NSLog(@"%@",[userData description]);
        
        id picture = result[@"picture"];
        id data = picture[@"data"];
        id url = data[@"url"];
        
        
        FBProfilePictureURL = url;
        FBEmail = [userData objectForKey:@"email"];
        FBFirstName = [userData objectForKey:@"first_name"];
        FBLastName = [userData objectForKey:@"last_name"];
        NSLog(@"FB EMAIL: %@", FBEmail);
        NSLog(@"FB FIRST NAME: %@", FBFirstName);
        NSLog(@"FB LAST NAME: %@", FBLastName);
        NSLog(@"FB PROFILE PIC URL: %@", FBProfilePictureURL);
                

        // Prevents the bug - pushing the vc twice or more.
        
        if (!createProfileIsPushed)
        {
            NSLog(@"createProfileIsPushed: %hhd", createProfileIsPushed);
            CreateProfileViewController *createProfile = [[CreateProfileViewController alloc] init];
            [self.navigationController pushViewController:createProfile animated:YES];
            
            createProfileIsPushed = YES;
        }

    }];


}

The code is is pretty simple, in fact, you can just copy and pasted it, except the last part of the code, starting from the "//prevents the bug". I am pushing a new ViewController here, however, the viewcontroller was being pushed twice or more, so I just made a quite dirty way, is it dirty? So, there you go. All the data (well technically not all the data) from Facebook is being sent here. Check out my NSLogs in my code. If you want something more to be fetched, just add it in the dictionaryWithObjectsAndKeys. Contact me if you don't understand anything here.

Post a Comment