- (IBAction)LoginViaFb:(id)sender {
// Set permissions required from the facebook user account
NSArray *permissionsArray = @[ @"user_email", @"user_relationships", @"user_birthday", @"user_location"];
// Login PFUser using facebook
[PFFacebookUtils logInWithPermissions:permissionsArray block:^(PFUser *user, NSError *error) {
NSLog(@"user=%@",user);
if (!user) {
if (!error) {
NSLog(@"Uh oh. The user cancelled the Facebook login.");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Log In Error" message:@"Uh oh. The user cancelled the Facebook login." delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Dismiss", nil];
[alert show];
} else {
NSLog(@"Uh oh. An error occurred --- %@", error);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Log In Error" message:[error description] delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Dismiss", nil];
[alert show];
}
} else if (user.isNew) {
NSLog(@"User with facebook signed up and logged in!");
} else {
NSLog(@"User with facebook logged in!");
}
// If the user is already logged in, display any previously cached values before we get the latest from Facebook.
if ([PFUser currentUser]) {
[self updateProfile];
}
// Send request to Facebook
FBRequest *request = [FBRequest requestForMe];
[request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
// handle response
if (!error) {
// Parse the data received
NSDictionary *userData = (NSDictionary *)result;
NSLog(@"userData -------- %@",userData);
NSString *facebookID = userData[@"id"];
NSMutableDictionary *userProfile = [NSMutableDictionary dictionaryWithCapacity:7];
if (facebookID) {
userProfile[@"facebookId"] = facebookID;
}
if (userData[@"name"]) {
userProfile[@"name"] = userData[@"name"];
}
if (userData[@"location"][@"name"]) {
userProfile[@"location"] = userData[@"location"][@"name"];
}
if (userData[@"gender"]) {
userProfile[@"gender"] = userData[@"gender"];
}
if (userData[@"birthday"]) {
userProfile[@"birthday"] = userData[@"birthday"];
}
if (userData[@"relationship_status"]) {
userProfile[@"relationship"] = userData[@"relationship_status"];
}
[[PFUser currentUser] setObject:userProfile forKey:@"profile"];
[[PFUser currentUser] saveInBackground];
[self updateProfile];
} else if ([[[[error userInfo] objectForKey:@"error"] objectForKey:@"type"]
isEqualToString: @"OAuthException"]) { // Since the request failed, we can check if it was due to an invalid session
NSLog(@"The facebook session was invalidated");
} else {
NSLog(@"Some other error: %@", error);
}
}];
}];
编写此代码后,我的日志显示:
用户=(空)
哦,哦。发生错误--- Error Domain = com.facebook.sdk Code = 2“无法完成操作。(com.facebook.sdk error 2.)”UserInfo = 0xbf6f0c0 {com.facebook.sdk:ErrorLoginFailedReason = com.facebook.sdk:SystemLoginDisallowedWithoutError ,com.facebook.sdk:ErrorSessionKey =,expirationDate:(null),refreshDate:(null),attemptsRefreshDate:0001-12-30 00:00:00 +0000,permissions:(null)>}
< / LI>请帮忙,我在哪里做错了?并提前致谢。