如何使用Facebook Graph API和facebook ios sdk获取完整的User对象?

时间:2010-11-05 21:44:32

标签: ios facebook facebook-graph-api facebook-ios-sdk

我正在使用Facebook iOS SDK和Facebook Graph API。当我请求“我”时,它应该返回当前用户的所有属性,我实际上只获得了User对象的一小部分。我做错了什么?

我登录时也没有收到“允许/不允许”对话框,但也许是因为我是应用程序所有者?

在我的标题中:

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

@interface FacebookAppViewController : UIViewController <FBRequestDelegate, FBDialogDelegate, FBSessionDelegate> {
    Facebook* facebook;
    NSArray* permissions;

    IBOutlet UILabel* JSONLabel;
}

@property (retain, nonatomic) UILabel* JSONLabel;

- (IBAction)login:(id)sender;
- (IBAction)logout:(id)sender;

@end

在我的实施中:

#import "FacebookAppViewController.h"

static NSString* apiKey = @""; // with my app ID

@implementation FacebookAppViewController

@synthesize JSONLabel;

- (IBAction)login:(id)sender
{
    [facebook authorize:apiKey permissions:permissions delegate:self];
}

- (void) fbDidLogin
{
    [facebook requestWithGraphPath:@"me" andDelegate:self];
}

- (void)request:(FBRequest*)request didLoad:(id)result
{
    NSString* uid = [result objectForKey:@"id"];

    if ([result isKindOfClass:[NSDictionary class]])
    {
        NSString* text = @"";
        NSDictionary* hash = result;

        for (NSString* key in hash)
        {
            text = [text stringByAppendingFormat:@"\n%@: ", key];

            NSObject* value = [hash objectForKey:key];
            if (value == nil)
            {
                NSLog(@"value is nil");
                continue;
            }

            if ([value isKindOfClass:[NSString class]])
            {
                NSString* str = value;
                text = [text stringByAppendingFormat:@"%@", str];
            }
            JSONLabel.text = text;
        }
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];

    facebook = [[Facebook alloc] init];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        permissions = [[NSArray arrayWithObjects:@"read_stream", @"offline_access", nil] retain];
    }
    return self;
}

1 个答案:

答案 0 :(得分:3)

使用fields参数和任何记录的用户字段。 @ “我?场= ID,姓名,性别,...”