Chris
Chris

Reputation: 12181

Getting Facebook ID number with API

I know - I have read the documentation - but frankly Facebook is horrible at it. I can't figure out how to send a request that illicits a response. Here's what I'm trying:

 NSURL *fbID = [NSURL URLWithString:@"https://graph.facebook.com/me?fields=id"];
fbUserID = [[NSData alloc] initWithContentsOfURL: fbID];

Where should this code go? Is there a specific class it is looking for if I have set 'self' as delegate?

Upvotes: 1

Views: 2513

Answers (1)

Matt
Matt

Reputation: 1563

How are you trying to integrate FB into your app? I have been looking into Facebook connect recently and found their documentation very helpful (http://developers.facebook.com/docs/guides/mobile/#ios). I know you said you looked at the documentation, but for me after going through their steps for iOS I was able to make an app that allowed users to sign in and give me access to their basic information, including ID. After going through authorization, I had the following code in my app delegate:

- (void) fbDidLogin {
    //get information about the currently logged in user
    [facebook requestWithGraphPath:@"me" andDelegate:self];
}

- (void)request:(FBRequest *)request didLoad:(id)result {
    NSString *name = [result objectForKey:@"name"];
    NSString *msg = [[NSString alloc] initWithFormat:@"To prove this works, your name is %@",name];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Thanks for Logging In!" 
                                                    message:msg 
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
    [alert release];
}

This gets the user's full name after logging in and displays it. You can do the same thing for getting their ID.

Upvotes: 3

Related Questions