检索Facebook页面喜欢使用自定义loginbutton

我已经下载了这个示例项目用于与Facebook自定义login:

FBLoginCustomUISample

并作为一个testing,我想实现这个方法发现在Facebook的SDK来检索用户页面喜欢:

/* make the API call */ [FBRequestConnection startWithGraphPath:@"/me/likes" parameters:nil HTTPMethod:@"GET" completionHandler:^( FBRequestConnection *connection, id result, NSError *error ) { NSLog(@"%@",result); }]; 

我已经复制粘贴在CustomLoginViewController.m像这样:

 - (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); }]; }]; } } 

我login时会返回一个空的数据。

感谢您的帮助,请裸露在我身边,因为这是我第一次尝试使用ios sdk。

在facebook提供的示例中,我尝试在SignUpSetProfileDetailsViewController.m类中实现下一个代码:

 - (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]; } 

button是以编程方式添加的,我所做的就是丢失appdelegate实现

将user_likes添加到权限

  NSArray *permissions = [[NSArray alloc] initWithObjects: @"public_profile", @"user_likes", nil]; 

虽然如果单独pipe理权限列表,它会更好,因为有许多权限(包括读写权限),您需要pipe理以获取各种数据

更新了buttonfunction,现在它的工作原理:

 - (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 NSArray *permissions = [[NSArray alloc] initWithObjects: @"public_profile", @"user_likes", nil]; [FBSession openActiveSessionWithReadPermissions:permissions allowLoginUI:YES completionHandler: ^(FBSession *session, FBSessionState state, NSError *error) { /* make the API call */ [FBRequestConnection startWithGraphPath:@"/me/likes" parameters:nil HTTPMethod:@"GET" completionHandler:^( FBRequestConnection *connection, id result, NSError *error ) { NSLog(@"%@",result); }]; // 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]; }]; } } 

另外我学到的是,在开发人员的Facebook帐户,在应用程序,你想检索user_likes,user_interests和其他类似的Facebookgraphicsapi请求,你需要添加到用户的权限,通过Facebook上的状态和评论,并提交新的审查关于许可,你需要什么以及将如何被起诉。