Mike
Mike

Reputation: 119

Grab facebook user ID and pass from iOS app to MySQL?

I have developed an app that successfully uses Facebook Connect to allow the user to log in. Once logged in, the user has the ability to upload photos to my server. The URL link to these photos/images gets stored in MySQL. I need to link their facebook user ID to their photos in my database. So my questions are:

How do I grab the user ID in Xcode? How do I then pass it thru to MySQL? What does all of this code look like?

In response to Joel's answer, how would I add the Facebook credentials to NSUserDefaults? I know how to store one value (below), but how do I do multiple values for the facebookID, userName, and userEmail?

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"username"] == nil) //we haven't saved anything yet
    [defaults setObject:authenticatedUsersName forKey:@"username"];

Upvotes: 0

Views: 1320

Answers (1)

Joel
Joel

Reputation: 16134

There's nothing wrong with storing the Facebook User ID as the comments above suggest. The token can change between sessions and from device to device. Also if you have multiple apps (say web/android/iphone) you want the user to be recognized across devices. In fact, this is how you should uniquely reference a user.

Anyway, to get the fid do this:

On your fbDidLogin delegate method, add this line:

[facebook requestWithGraphPath:@"me" andDelegate:self];    

Then setup the request delegate like so:

- (void)request:(FBRequest *)request didLoad:(id)result {
    NSString *facebookId = [result objectForKey:@"id"];
    NSString *userName = [result objectForKey:@"name"];
    NSString *userEmail = [result objectForKey:@"email"]; 

    //do whatever you need to do with this info next 
    //(ie. save to db, pass to user singleton, whatever)

}

ps. the above code assumes you already have Facebook login implemented. I'm not going to post all that code since there is a step by step instruction for that part in their Getting Started documentation.

Upvotes: 2

Related Questions