我正在尝试在我的应用中加入Facebook登录功能,并且到目前为止还有以下两种方法:
#pragma mark - IBActions
- (IBAction)loginWithFacebookButtonPressed:(id)sender {
self.activityIndicator.hidden = NO;
[self.activityIndicator startAnimating];
NSArray *permissionsArray = @[@"public_profile", @"email", @"user_friends"];
FBSDKLoginManager *loginManager = [[FBSDKLoginManager alloc] init];
[loginManager logInWithReadPermissions:permissionsArray fromViewController:nil handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
self.activityIndicator.hidden = YES;
[self.activityIndicator stopAnimating];
if(!result){
if(!error){
UIAlertView *alertViewCancel = [[UIAlertView alloc] initWithTitle:@"Login Error" message:@"Login to Facebook was cancelled" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alertViewCancel show];
}else if(error){
UIAlertView *alertViewError = [[UIAlertView alloc] initWithTitle:@"Login Error" message:[error description] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alertViewError show];
}
}else{
[self updateUserInformation];
[self performSegueWithIdentifier:@"toTabBarController" sender:sender];
}
}];
}
#pragma mark - Helper Method
- (void) updateUserInformation{
FBRequest *request = [FBRequest requestForMe];
[request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if(!error){
NSDictionary *userDictionary = (NSDictionary *) result;
NSMutableDictionary *userProfile = [[NSMutableDictionary alloc] initWithCapacity:8];
if(userDictionary[@"name"]){
userProfile[@"name"] = userDictionary[@"name"];
}
if(userDictionary[@"first_name"]){
userProfile[@"first_name"] = userDictionary[@"first_name"];
}
if(userDictionary[@"location"][@"name"]){
userProfile[@"location"] = userDictionary[@"location"][@"name"];
}
if (userDictionary[@"gender"]) {
userProfile[@"gender"] = userDictionary[@"gender"];
}
if (userDictionary[@"birthday"]) {
userProfile[@"birthday"] = userDictionary[@"birthday"];
}
if (userDictionary[@"interested_in"]) {
userProfile[@"interested_in"] = userDictionary[@"interested_in"];
}
[[PFUser currentUser] setObject:userProfile forKey:@"profile"];
[[PFUser currentUser] saveInBackground];
}else{
NSLog(@"Error in Facebook Request: %@", [error description]);
}
}];
}
我的问题是,当我使用Facebook登录时(在我的loginWithFacebookButtonPressed方法中),我被告知会自动为登录Facebook的当前用户创建PFUser对象。在我的帮助方法(updateUserInformation)中,我试图从Facebook获取用户的信息,并在解析时存储该PFUser的信息。不幸的是,由于PFUser对象(对于使用我的应用程序登录Facebook的用户)没有自动创建,因此现在[PFUser currentUser]将无法工作。有人能告诉我如何为使用我的应用程序登录Facebook的用户创建PFUser对象吗?