Facebook iOS SDK:如何获取用户的Facebook状态?

发布于 2025-01-02 05:11:12 字数 257 浏览 0 评论 0原文

我在使用 Facebook iOS SDK 参考时遇到了很大的困难,因为我不是一名专家编码员。我只有这个简单的问题。

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

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

但它去了哪里?比方说,我如何获取用户的名字或状态并将其设置为标签的值?

如果有人给我写下完整的代码作为答案,我将感激不尽。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

许久 2025-01-09 05:11:12

当您调用此方法时,SDK 期望如下:

  • self 实现 FBRequestDelegate
  • self 有一个方法 request: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 ----

希望这会有所帮助!

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!

司马昭之心 2025-01-09 05:11:12

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

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)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文