Umair Khan Jadoon
Umair Khan Jadoon

Reputation: 2914

Facebook iOS SDK: how to get user's Facebook status?

I am getting very hardtime using the Facebook iOS SDK reference because I am not an expert coder. I just have this simple question.

I know the following code brings me the user information...

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

but where does it go? How can I, let's say, get the User's First Name or status and set it up as the value of a label?

I would be thankful gazillion times if someone writes me the whole code in answer.

Upvotes: 1

Views: 2964

Answers (2)

karthick
karthick

Reputation: 371

Umair,

After getting the access token you can use this code:

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://graph.facebook.com/me?access_token=youracesstoken"]];

NSError *err = nil;

NSURLResponse *resp = nil;

NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:&resp error:&err];


if (resp != nil) {

    NSString *stringResponse = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];

    NSLog(@"stringResponse---%@",stringResponse);

    // stringResponse will be in JSON Format you need to use a JSON parser (Like SBJSON or TouchJSON)
}

Upvotes: 1

Simon Germain
Simon Germain

Reputation: 6844

When you call this method, here's what the SDK expects:

  • self implements FBRequestDelegate
  • self has a method request:didLoad

Here's a quick code sample:

---- MyClass.h BEGIN ----

#import <UIKit/UIKit.h>
#import "FBConnect.h"

@interface MyClass : NSObject <FBSessionDelegate, FBRequestDelegate>

@property (nonatomic, retain) Facebook *facebook;
@property (nonatomic, retain) NSString *userStatus;

@end

---- MyClass.h END ----

---- MyClass.m BEGIN ----

#import "MyClass.h"

@implementation MyClass

@synthesize facebook;
@synthesize userStatus;

- (id)init {
    self = [super init];

    if (self) {
        facebook = [[Facebook alloc] initWithAppId:@"App ID Here" andDelegate:self];
    }

    return self;
}

- (void)fbDidLogin {
    NSLog(@"Facebook logged in!");
    [facebook requestWithGraphPath:@"me" andDelegate:self];
}

- (void)request:(FBRequest *)request didLoad:(id)result {
    NSLog(@"Request loaded! Result: %@", result);
    SBJSON *parser = [[SBJSON alloc] init];
    NSDictionary *jsonResponse = [parser objectWithString:result error:nil];
    userStatus = [jsonResponse objectForKey:message];
    [parser release];

    NSLog(@"User's status message: %@", userStatus);
}

@end

---- MyClass.m END ----

Hope this helps!

Upvotes: 2

Related Questions