通过Facebook iOS Parse.comlogin用户

我正尝试通过我的parse.com应用程序中的Facebooklogin一个用户。 我拥有所有的ID和appdelegate方法。

我在故事板中创build了一个视图,并创build了一个Facebookloginbutton,然后将其作为IBAction连接到我的.h文件。

我的代码:

- (IBAction)fblogin:(FBSDKLoginButton *)sender { [PFFacebookUtils logInInBackgroundWithReadPermissions:@[@"public_profile", @"email"] block:^(PFUser *user, NSError *error) { if (error) { // NSLog(@"Uh oh. The user cancelled the Facebook login."); UIAlertView *alertVeiw = [[UIAlertView alloc] initWithTitle:@"Sorry" message:[error.userInfo objectForKey:@"error"] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alertVeiw show]; } else if (!user) { UIAlertView *alertVeiw = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"You cancelled Login, try again!" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alertVeiw show]; }else { // NSLog(@"User logged in through Facebook!"); // [self dismissViewControllerAnimated:YES completion:NULL]; FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"first_name, last_name, email, public_profile"}]; [request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) { if (error) { UIAlertView *alertVeiw = [[UIAlertView alloc] initWithTitle:@"Sorry" message:[error.userInfo objectForKey:@"error"] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alertVeiw show]; } else if ([[error userInfo][@"error"][@"type"] isEqualToString: @"OAuthException"]) { // Since the request failed, we can check if it was due to an invalid session // NSLog(@"The facebook session was invalidated"); [PFFacebookUtils unlinkUserInBackground:[PFUser currentUser]]; } else { NSDictionary *userData = (NSDictionary *)result; // [self requestFacebookUser:user]; NSString *name = userData[@"name"]; NSString *email = userData[@"email"]; user.username = name; user.email = email; [user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { if (error) { UIAlertView *alertVeiw = [[UIAlertView alloc] initWithTitle:@"Sorry" message:[error.userInfo objectForKey:@"error"] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alertVeiw show]; } else { // [self dismissViewControllerAnimated:NO completion:nil]; //[self.navigationController popToRootViewControllerAnimated:NO]; [self performSegueWithIdentifier:@"inbox" sender:self]; } }]; } }]; } }]; } 

便笺簿网页打开时,我按loginFacebookbutton,然后我们login,然后没有任何反应。

请帮我实施一步一步的facebook登陆程序。

编辑:

应用程序委托:

 [PFFacebookUtils initializeFacebookWithApplicationLaunchOptions:launchOptions]; - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { return [[FBSDKApplicationDelegate sharedInstance] application:application openURL:url sourceApplication:sourceApplication annotation:annotation]; } applicationWillEnterForeGround: [FBSDKAppEvents activateApp]; 

然后:

随着你的(SanitLee)答案我有这些方法:

 - (void)loginViewFetchedUserInfo:(FBSDKLoginManager *)loginView user:(FBSDKProfile*)user { } -(void)loginButton:(FBSDKLoginButton *)loginButton didCompleteWithResult:(FBSDKLoginManagerLoginResult *)result error:(NSError *)error{ // [self performSegueWithIdentifier:@"inbox" sender:self]; } -(void)loginButtonDidLogOut:(FBSDKLoginButton *)loginButton{ } -(void)loginButtonClicked{ } 

我也得到这个错误,其视图不在窗口层次结构!

以下是我如何做到这一点,它对我有用。 首先,在你的m文件中input:

 #import <ParseFacebookUtils/PFFacebookUtils.h>//fb login for parse 

那么下面的代码应该做你想要的:

 - (IBAction)loginButtonTouchHandler:(id)sender { NSArray *permissionsArray = @[ @"user_about_me", @"user_relationships", @"user_birthday", @"user_location"]; [MBProgressHUD showHUDAddedTo:self.view animated:YES]; [PFFacebookUtils logInWithPermissions:permissionsArray block:^(PFUser *user, NSError *error) { if (!user) { NSString *errorMessage = nil; if (!error) { NSLog(@"Uh oh. The user cancelled the Facebook login."); errorMessage = NSLocalizedString(@"Uh oh. The user cancelled the Facebook login.", nil); } else { NSLog(@"Uh oh. An error occurred: %@", error); errorMessage = [error localizedDescription]; } UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Log In Error", nil) message:errorMessage delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"Dismiss", nil), nil]; [MBProgressHUD hideHUDForView:self.view animated:YES]; [alert show]; } else { if (user.isNew) { NSLog(@"User with facebook signed up and logged in!"); [self _loadData]; } else { NSLog(@"User with facebook logged in!"); } [MBProgressHUD hideHUDForView:self.view animated:YES]; [self _presentNextViewControllerAnimated:YES]; } }]; } - (void)_presentNextViewControllerAnimated:(BOOL)animated { PAWWallViewController *wallViewController = [[PAWWallViewController alloc] initWithNibName:nil bundle:nil]; [(UINavigationController *)self.presentingViewController pushViewController:wallViewController animated:NO]; [self.presentingViewController dismissModalViewControllerAnimated:YES]; } - (void)_loadData { FBRequest *request = [FBRequest requestForMe]; [request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) { if (!error) { PFUser *user = [PFUser currentUser]; NSDictionary *userData = (NSDictionary *)result; NSString *facebookID = userData[@"id"]; NSString *name = userData[@"name"]; NSString *email = userData[@"email"]; NSString *location = userData[@"location"][@"name"]; NSString *gender = userData[@"gender"]; NSString *birthday = userData[@"birthday"]; NSString *relationship = userData[@"relationship_status"]; NSString *facebookLink = [NSString stringWithFormat:@"Facebook.com/%@", facebookID]; NSURL *pictureURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=large&return_ssl_resources=1", facebookID]]; NSLog(@"facebookID --> %@", facebookID); NSLog(@"name --> %@", name); NSLog(@"email --> %@", email); //for profile image NSURLRequest *urlRequest = [NSURLRequest requestWithURL:pictureURL]; // Run network request asynchronously [NSURLConnection sendAsynchronousRequest:urlRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { if (connectionError == nil && data != nil) { PFFile *userImageFile = [PFFile fileWithName:@"userImage.jpg" data:data]; if (userImageFile) [user setObject:userImageFile forKey:kPAWParseUserImageKey]; } [user setObject:facebookID forKey:kPAWParseUsernameKey]; //initially use fb id as username to avoid duplication [user setObject:name forKey:kPAWParseRealnameKey]; [user setObject:facebookLink forKey:kPAWParseFacebookKey]; [user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { if (!error) { if (succeeded) { NSLog(@"fb user saved successfully"); } } else { NSLog(@"fb user saved unsuccessfully"); } }]; }]; } else if ([[[[error userInfo] objectForKey:@"error"] objectForKey:@"type"] isEqualToString: @"OAuthException"]) { NSLog(@"The facebook session was invalidated"); UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Log out of Spotpost?", nil) message:nil delegate:self cancelButtonTitle:NSLocalizedString(@"Log out", nil) otherButtonTitles:NSLocalizedString(@"Cancel", nil), nil]; [alertView show]; } else { NSLog(@"Some other error: %@", error); } }]; } 

以下是我在应用程序代理中进行的configuration:

 #import <ParseFacebookUtils/PFFacebookUtils.h> //fb login for parse .... - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { ... [PFFacebookUtils initializeFacebook]; //fb login for parse return YES; } - (void)applicationDidBecomeActive:(UIApplication *)application { .... [FBAppCall handleDidBecomeActiveWithSession:[PFFacebookUtils session]]; //fb login for parse } - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { return [FBAppCall handleOpenURL:url sourceApplication:sourceApplication withSession:[PFFacebookUtils session]]; } - (void)applicationWillTerminate:(UIApplication *)application { [[PFFacebookUtils session] close]; }