从 Facebook 读取信息
Posted
技术标签:
【中文标题】从 Facebook 读取信息【英文标题】:Reading information from Facebook 【发布时间】:2013-11-09 18:03:54 【问题描述】:我是 ios 的初学者。我正在尝试创建一个应用程序并在 *** 和其他一些网站中引用了很多帖子,我使用以下代码访问 Facebook 帐户。
AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
if (appDelegate.session.isOpen)
[appDelegate.session closeAndClearTokenInformation];
else
if (appDelegate.session.state != FBSessionStateCreated)
appDelegate.session = [[FBSession alloc] init];
[appDelegate.session openWithCompletionHandler:^(FBSession *session,
FBSessionState status,
NSError *error)
];
之后,在一个函数中,我使用以下代码访问用户的 Facebook 详细信息。
if (FBSession.activeSession.isOpen)
[[FBRequest requestForMe] startWithCompletionHandler:
^(FBRequestConnection *connection,
NSDictionary<FBGraphUser> *user,
NSError *error)
if (!error)
_FirstName.text = user.first_name;
regisrationdetails.fbid = user.id;
_LastName.text=user.last_name;
_EmailAddress.text=user.email;
NSArray *locationarray=[[NSArray alloc]initWithObjects:user.location,nil];
_City.text=[locationarray objectAtIndex:1];
NSLog(@"%@",user.first_name);
];
没有显示错误,但我无法从 Facebook 的文本字段中获取信息。如果擅长此操作的人知道如何访问信息,请帮助我。
【问题讨论】:
【参考方案1】:- (IBAction)getMeButtonTapped:(id)sender
if(!_accountStore)
_accountStore = [[ACAccountStore alloc] init];
ACAccountType *facebookTypeAccount = [_accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
[_accountStore requestAccessToAccountsWithType:facebookTypeAccount
options:@ACFacebookAppIdKey: @"571438296262222", ACFacebookPermissionsKey: @[@"email"]
completion:^(BOOL granted, NSError *error)
if(granted)
NSArray *accounts = [_accountStore accountsWithAccountType:facebookTypeAccount];
_facebookAccount = [accounts lastObject];
NSLog(@"Success");
[self me];
else
// ouch
NSLog(@"Fail");
NSLog(@"Error: %@", [error localizedDescription]);
];
- (void)me
NSURL *meurl = [NSURL URLWithString:@"https://graph.facebook.com/me"];
SLRequest *merequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:meurl
parameters:nil];
merequest.account = _facebookAccount;
[merequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
NSString *meDataString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"%@", meDataString);
];
使用此希望对您有所帮助,同时添加社交框架和帐户框架
【讨论】:
什么是_accountStore...?? 您的帐户信息的存储位置,即 ACAccountStore【参考方案2】:试试这个..
- (void)fetchFacebookUserInfo
if ( FBSession.activeSession.isOpen)
[FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection,
id<FBGraphUser> user,
NSError *error)
if (!error)
NSString *name = user.name;
NSString *userName = user.username;
NSString *firstName = user.first_name;
NSString *lastName = user.last_name;
NSString *email1 = [user objectForKey:@"email"];
NSString *birthday1 = user.birthday;
NSString *locale = [user objectForKey:@"locale"];
NSString *location = [user.location objectForKey:@"name"];
];
【讨论】:
以上是关于从 Facebook 读取信息的主要内容,如果未能解决你的问题,请参考以下文章
最好的 c# 语法/成语,从 Facebook 读取朋友数组