使用自定义登录按钮检索喜欢的Facebook页面 [英] Retrieve Facebook pages likes using custom login button
问题描述
我已经下载了此示例项目,以便通过Facebook自定义登录:
I have downloaded this sample project for custom login with Facebook:
并且作为测试,我想实现在facebook sdk上找到的此方法 检索用户喜欢的页面:
and as a test I want to implement this method found on facebook sdk to retrive users pages likes:
/* make the API call */
[FBRequestConnection startWithGraphPath:@"/me/likes"
parameters:nil
HTTPMethod:@"GET"
completionHandler:^(
FBRequestConnection *connection,
id result,
NSError *error
) {
NSLog(@"%@",result);
}];
我已经将其复制粘贴到CustomLoginViewController.m中,如下所示:
I have copy-pasted it in the CustomLoginViewController.m like so:
- (IBAction)buttonTouched:(id)sender
{
// If the session state is any of the two "open" states when the button is clicked
if (FBSession.activeSession.state == FBSessionStateOpen
|| FBSession.activeSession.state == FBSessionStateOpenTokenExtended) {
// Close the session and remove the access token from the cache
// The session state handler (in the app delegate) will be called automatically
[FBSession.activeSession closeAndClearTokenInformation];
// If the session state is not any of the two "open" states when the button is clicked
} else {
// Open a session showing the user the login UI
// You must ALWAYS ask for public_profile permissions when opening a session
[FBSession openActiveSessionWithReadPermissions:@[@"public_profile"]
allowLoginUI:YES
completionHandler:
^(FBSession *session, FBSessionState state, NSError *error) {
// Retrieve the app delegate
AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
// Call the app delegate's sessionStateChanged:state:error method to handle session state changes
[appDelegate sessionStateChanged:session state:state error:error];
[FBRequestConnection startWithGraphPath:@"/me/likes?limit=10"
parameters:nil
HTTPMethod:@"GET"
completionHandler:^(
FBRequestConnection *connection,
id result,
NSError *error
) {
NSLog(@"%@",result);
}];
}];
}
}
登录后,我得到一个空数据.
And I get returned an empty data when I LogIn.
在完成Facebook提供的示例中的方法后,我尝试在SignUpSetProfileDetailsViewController.m类中实现下一个代码:
After making the method in the example provided by facebook work I tryed to implement the next code in my SignUpSetProfileDetailsViewController.m class:
- (void)facebookButtonFunction{
// Open a session showing the user the login UI
// You must ALWAYS ask for public_profile permissions when opening a session
NSArray *permissions = [[NSArray alloc] initWithObjects:
@"public_profile",
@"user_interests ",
nil];
[FBSession openActiveSessionWithReadPermissions:permissions
allowLoginUI:YES
completionHandler:
^(FBSession *session, FBSessionState state, NSError *error) {
[FBRequestConnection startWithGraphPath:@"/me/interests"
parameters:nil
HTTPMethod:@"GET"
completionHandler:^(
FBRequestConnection *connection,
id result,
NSError *error
) {
NSLog(@"%@",result);
}];
// Retrieve the app delegate
// Call the app delegate's sessionStateChanged:state:error method to handle session state changes
[self sessionStateChanged:session state:state error:error];
}];
}
- (void)sessionStateChanged:(FBSession *)session state:(FBSessionState) state error:(NSError *)error
{
// If the session was opened successfully
if (!error && state == FBSessionStateOpen){
NSLog(@"Session opened");
// Show the user the logged-in UI
[self userLoggedIn];
return;
}
if (state == FBSessionStateClosed || state == FBSessionStateClosedLoginFailed){
// If the session is closed
NSLog(@"Session closed");
// Show the user the logged-out UI
[self userLoggedOut];
}
// Handle errors
if (error){
NSLog(@"Error");
NSString *alertText;
NSString *alertTitle;
// If the error requires people using an app to make an action outside of the app in order to recover
if ([FBErrorUtility shouldNotifyUserForError:error] == YES){
alertTitle = @"Something went wrong";
alertText = [FBErrorUtility userMessageForError:error];
[self showMessage:alertText withTitle:alertTitle];
} else {
// If the user cancelled login, do nothing
if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryUserCancelled) {
NSLog(@"User cancelled login");
// Handle session closures that happen outside of the app
} else if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryAuthenticationReopenSession){
alertTitle = @"Session Error";
alertText = @"Your current session is no longer valid. Please log in again.";
[self showMessage:alertText withTitle:alertTitle];
// For simplicity, here we just show a generic message for all other errors
// You can learn how to handle other errors using our guide: https://developers.facebook.com/docs/ios/errors
} else {
//Get more error information from the error
NSDictionary *errorInformation = [[[error.userInfo objectForKey:@"com.facebook.sdk:ParsedJSONResponseKey"] objectForKey:@"body"] objectForKey:@"error"];
// Show the user an error message
alertTitle = @"Something went wrong";
alertText = [NSString stringWithFormat:@"Please retry. \n\n If the problem persists contact us and mention this error code: %@", [errorInformation objectForKey:@"message"]];
[self showMessage:alertText withTitle:alertTitle];
}
}
// Clear this token
[FBSession.activeSession closeAndClearTokenInformation];
// Show the user the logged-out UI
[self userLoggedOut];
}
}
- (void)userLoggedOut
{
// Set the button title as "Log in with Facebook"
[facebookButtonLabel setText:@"Connect"];
[facebookButtonLabel setTextColor:[UIColor whiteColor]];
}
// Show the user the logged-in UI
- (void)userLoggedIn
{
// Set the button title as "Log out"
[facebookButtonLabel setText:@"Connected"];
[facebookButtonLabel setTextColor:[UIColor yellowColor]];
// Welcome message
[self showMessage:@"You're now logged in with Facebook!" withTitle:@"Welcome!"];
}
- (void)showMessage:(NSString *)text withTitle:(NSString *)title
{
[[[UIAlertView alloc] initWithTitle:title
message:text
delegate:self
cancelButtonTitle:@"OK!"
otherButtonTitles:nil] show];
}
以编程方式添加按钮的地方,我要做的是丢失appdelegate实现
where the button is added programatically, what I do is to lose the appdelegate implementation
推荐答案
向权限添加user_likes
Add user_likes to permissions
NSArray *permissions = [[NSArray alloc] initWithObjects:
@"public_profile",
@"user_likes",
nil];
如果单独管理权限列表会更好,因为要获取各种数据,需要管理大量的权限(包括读写权限)
Although its better if you manage the permissions list seperately, since there is a whole host of permissions (both read and write) that you will need to manage for getting various data
这篇关于使用自定义登录按钮检索喜欢的Facebook页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!