我正在尝试开发一个简单的应用程序,当用户连接到Facebook时,它会从Facebook检索数据。 我试过这个代码。
NSArray *permissions = [[NSArray alloc] initWithObjects:@"user_birthday",@"user_hometown",@"user_location",@"email",@"basic_info", nil];
[FBSession openActiveSessionWithReadPermissions:permissions
allowLoginUI:YES
completionHandler:^(FBSession *session,
FBSessionState status,
NSError *error) {
}];
[FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
NSLog(@"%@", [result objectForKey:@"gender"]);
NSLog(@"%@", [result objectForKey:@"hometown"]);
NSLog(@"%@", [result objectForKey:@"birthday"]);
NSLog(@"%@", [result objectForKey:@"email"]);
}];
但是当我运行这段代码时,它会给出一个错误“FBSDKLog:对端点请求的错误'我':必须为调用此端点指定一个开放的FBSession。”
提前致谢,非常感谢您的帮助。
答案 0 :(得分:6)
错误是非常合适的,它试图说的是一旦会话打开就应该调用请求连接方法。 现在你的
[FBSession openActiveSessionWithReadPermissions:permissions
allowLoginUI:YES
completionHandler:^(FBSession *session,
FBSessionState status,
NSError *error) {
}];
方法返回BOOL值true或false以指定您是否打开会话(它尝试同步打开)。因此,首先检查此调用的结果,并将其放在代码中以获取信息。例如。
if (FBSession.activeSession.isOpen)
{
[FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
NSLog(@"%@", [result objectForKey:@"gender"]);
NSLog(@"%@", [result objectForKey:@"hometown"]);
NSLog(@"%@", [result objectForKey:@"birthday"]);
NSLog(@"%@", [result objectForKey:@"email"]);
}];
}
这应该删除你的错误,但是你仍然可能无法得到结果。你可能会或者可能不会在第一次调用此代码时得到结果但是每当调用完成处理程序的代码时,此方法FBRequestConnection也会得到调用,当时你会得到结果,因为它是异步调用。
如果仍然无效,请尝试此
if (FBSession.activeSession.isOpen)
{
[[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
if (error)
{
NSLog(@"error:%@",error);
}
else
{
// retrive user's details at here as shown below
NSLog(@"FB user first name:%@",user.first_name);
NSLog(@"FB user last name:%@",user.last_name);
NSLog(@"FB user birthday:%@",user.birthday);
}
}];
答案 1 :(得分:1)
`(void)fbAccountConfigureWithBlock:(void(^)(id,NSString *))block { _block_data =块;
if(![SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{
dispatch_async(dispatch_get_main_queue(), ^{
[self showAlertMessage:@"" message:@"Please go to settings and add at least one facebook account."];
_block_data(nil,nil);
});
return;
}
ACAccountStore *store = [[ACAccountStore alloc]init];
ACAccountType *accountType = [store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
[store requestAccessToAccountsWithType:accountType
options:@{ACFacebookAppIdKey : FacebookAppId,
ACFacebookAudienceKey : ACFacebookAudienceFriends,
ACFacebookPermissionsKey : @[@"email"]}
completion:^(BOOL granted, NSError *error)
{
if(granted){
NSArray *array = [store accountsWithAccountType:accountType];
if(!array.count){
dispatch_sync(dispatch_get_main_queue(), ^{
[self showAlertMessage:@"" message:@"Please go to settings and add at least one facebook account."];
_block_data(nil,nil);
});
}
else{
ACAccount *account = array[0];
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:[NSURL URLWithString:@"https://graph.facebook.com/me"]
parameters: @{@"fields":@"id,first_name,last_name,name,email,picture.height(180).width(180)"}];
[request setAccount:account];
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
{
if(!error){
NSDictionary *userData = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
NSLog(@"Facebook user data ----> %@",userData);
dispatch_async(dispatch_get_main_queue(), ^{
if(userData[@"error"] != nil)
[self attemptRenewCredentials:store account:account];
else
_block_data(userData,nil);
});
}
else{
dispatch_async(dispatch_get_main_queue(), ^{
[self showAlertMessage:@"" message:error.localizedDescription];
_block_data(nil,nil);
});
}
}];
}
}
else
{
dispatch_async(dispatch_get_main_queue(), ^{
[self showAlertMessage:@"" message:@"We need permission to access your facebook account in order make registration."];
_block_data(nil,nil);
});
}
}];
}`