问题
I'm using a pre-built login UIButton of Version 2.3 of facebook integration.
Problem: Getting null result, when fetching user details.
-(void) loginButton:(FBSDKLoginButton *)loginButton didCompleteWithResult:(FBSDKLoginManagerLoginResult *)result error:(NSError *)error
{
if ([FBSDKAccessToken currentAccessToken]) {
FBSDKGraphRequest *request =[[FBSDKGraphRequest alloc]initWithGraphPath:@"me" parameters:nil];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
id result,
NSError *error)
{
// Handle the result
NSLog(@"%@",result);
}];
FBSDKProfile *profile = [[FBSDKProfile alloc]init];
}
}
回答1:
try this code:
AppDelegate.m
- (void)applicationDidBecomeActive:(UIApplication *)application {
[FBSDKAppEvents activateApp];
//Default FB Button
[[NSNotificationCenter defaultCenter]postNotificationName:@"getFacebookData" object:nil];
}
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
return [[FBSDKApplicationDelegate sharedInstance] application:application
openURL:url
sourceApplication:sourceApplication
annotation:annotation];
}
ViewDidLoad of viewController
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(getFacebookData) name:@"getFacebookData" object:nil];
FBSDKLoginButton *loginButton = [[FBSDKLoginButton alloc] init];
loginButton.center = self.view.center;
[self.view addSubview:loginButton];
Add this method in view controller
- (void)getFacebookData{
if ([FBSDKAccessToken currentAccessToken]) {
[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"first_name, last_name, picture.type(large), email, name, id, gender"}]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSLog(@"fetched user:%@", result);
}
}];
}
}
回答2:
Please follow this, you will get your answer To get profile picture -
FBSDKProfilePictureView *profilePictureview = [[FBSDKProfilePictureView alloc]initWithFrame:_imageView.frame];
[profilePictureview setProfileID:result[@"id"]];
[self.view addSubview:profilePictureview];
To get profile picture and email information
[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"picture, email"}] startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id res, NSError *error)
{
if (!error) {
NSString *pictureURL = [NSString stringWithFormat:@"%@",[res objectForKey:@"picture"]];
NSLog(@"the EMail = %@", [res objectForKey:@"email"]);
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:pictureURL]];
_imageView.image = [UIImage imageWithData:data];
}
else
{
NSLog(@"%@", [error localizedDescription]);
}}];
来源:https://stackoverflow.com/questions/30256089/how-can-i-get-user-information-using-facebook-sdk-4-1-in-ios