通过 Facebook iOS Parse.com 登录用户
Posted
技术标签:
【中文标题】通过 Facebook iOS Parse.com 登录用户【英文标题】:Login user through Facebook iOS Parse.com 【发布时间】:2015-12-30 21:31:40 【问题描述】:我正在尝试在我的 parse.com 应用程序中通过 facebook 登录用户。我有所有的 ids 和 appdelegate 方法。
我在情节提要中创建了一个视图并将其设为 Facebook 登录按钮,然后我将其作为 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];
];
];
];
当我按下登录 facebook 按钮然后我们登录时,facebook 网页打开,然后没有任何反应。
请帮我一步一步正确实现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 文件中:
#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);
];
这是我在应用程序委托中进行配置的方式:
#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];
【讨论】:
感谢您的回答,但它仍然对我不起作用,请参阅上面的问题以了解我的方法以及我在应用程序委托中拥有的内容 一旦用户登录,我们会进入收件箱 2 秒钟,然后它会返回登录。 @SanitLee 后端也没有创建用户 我的意思实际上是去设置 -> Facebook -> 你的名字 -> 然后删除你的帐户并再试一次。它曾经对我有用。以上是关于通过 Facebook iOS Parse.com 登录用户的主要内容,如果未能解决你的问题,请参考以下文章
Ios Parse 框架和 Facebook clientKey
facebook 登录是不是要求我在我的应用程序中使用 PFUser(使用 parse.com)?