我使用Facebook iOS SDK参考非常困难,因为我不是专家编码员。我只是有这个简单的问题。
我知道以下代码为我提供了用户信息......
[facebook requestWithGraphPath:@"me" andDelegate:self];
但它去哪儿了?我可以说,如何获得用户的名字或状态并将其设置为标签的值?
如果有人写完答案的全部代码,我会感激万亿。
答案 0 :(得分:2)
当您调用此方法时,这是SDK期望的内容:
以下是一个快速代码示例:
---- 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 :(得分: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)
}