我是iOS的初学者。我正在尝试创建一个应用程序,并在stackoverflow和其他一些网站中引用了很多帖子,我使用以下代码访问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获取文本字段的信息。如果有人知道如何获取信息,那么请帮助我。
答案 0 :(得分: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);
}];}
使用此希望可以帮助您,还可以添加社交框架以及帐户框架
答案 1 :(得分:1)
试试这个..
- (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"];
}
}];
}