2012-02-02 98 views
1

因为我不是专家的编码器,所以我很难使用Facebook iOS SDK参考。我只是有这个简单的问题。Facebook iOS SDK:如何获取用户的Facebook状态?

我知道下面的代码带给我的用户信息...

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

,但它在哪里去了?我怎么能说,获得用户的名字或地位,并将其设置为标签的价值?

如果有人在回答中给我写了整个代码,我会感激gazillion次。

+0

你是否还在寻找一个解决方案? – 2012-02-02 19:59:08

回答

2

当你调用这个方法,这里的SDK所期待的:

  • 自我实现FBRequestDelegate
  • 自我有一个方法要求:didLoad

下面是一个简单的代码示例:

---- 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 ---- 

希望这有助于!

1

Umair,

获得访问令牌,你可以使用此代码后:

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) 
}