Получаване на публичен профил във facebook на човек в приложението за ios

Работя върху приложение за ios, което изисква влизане във Facebook. Успешно внедрих процеса на влизане. Но сега не мога да намеря как и къде мога да получа информация за потребителския профил като собствено име, фамилия, профилна снимка и т.н. ..Моето приложение има разрешения за достъп до собствено име, фамилия, профилна снимка и имейл.


person Junior Bill gates    schedule 06.04.2015    source източник
comment
developers.facebook.com/docs/ios/graph   -  person Nik Kov    schedule 04.10.2016


Отговори (2)


По-долу е кодът за получаване на public_profile на човек с помощта на последния Facebook SDK v4.0.1. Трябва да използвате FBSDKProfile, за да получите профил на човек.

-(void)viewDidLoad{
        FBSDKLoginButton *loginButton = [[FBSDKLoginButton alloc] init];
        [self.view addSubview:loginButton];

        self.loginButton.readPermissions = @[@"public_profile", @"email", @"user_friends"];
        self.loginButton.delegate = self;
        [FBSDKProfile enableUpdatesOnAccessTokenChange:YES];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(profileUpdated:) name:FBSDKProfileDidChangeNotification object:nil];

         }

    -(void)profileUpdated:(NSNotification *) notification{
         NSLog(@"User name: %@",[FBSDKProfile currentProfile].name);
         NSLog(@"User ID: %@",[FBSDKProfile currentProfile].userID);
    }

    - (void)  loginButton:(FBSDKLoginButton *)loginButton
    didCompleteWithResult:(FBSDKLoginManagerLoginResult *)result
                    error:(NSError *)error{

    }

    - (void)loginButtonDidLogOut:(FBSDKLoginButton *)loginButton{

    }
person Sivajee Battina    schedule 06.04.2015
comment
Тази функция изобщо не се извиква - (void) loginButton:(FBSDKLoginButton *)loginButton didCompleteWithResult:(FBSDKLoginManagerLoginResult *)result error:(NSError *)error - person Junior Bill gates; 06.04.2015
comment
Моля, уверете се, че сте включили ‹FBSDKLoginButtonDelegate› в @interface .h файл - person Sivajee Battina; 06.04.2015
comment
да, вече го включих, но функцията не беше извикана. - person Junior Bill gates; 06.04.2015
comment
вашата нова редакция, позволете ми да извлека потребителския идентификатор и име, благодаря. Моля, кажете ми как мога да извлека имейл и профилна снимка - person Junior Bill gates; 06.04.2015
comment
Класът FBSDKProfilePictureView предоставя лесен начин за добавяне на нечие изображение на профил във Facebook към вашия изглед. Просто задайте свойството profileID на екземпляра. Можете да го зададете на стойност от мен, за да проследявате автоматично лицето, което е влязло в момента. - person Sivajee Battina; 08.04.2015
comment
как се справяте с резултата? - person SleepsOnNewspapers; 05.08.2015

Използвайте метода за известяване, така че когато получи информация от facebook, да може да извика делегиран метод на facebook

 [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(handleFBSessionStateChangeWithNotification:)
                                                 name:@"SessionStateChangeNotification"
                                               object:nil];

След това приложете този метод

-(void)handleFBSessionStateChangeWithNotification:(NSNotification *)notification{
 NSDictionary *userInfo = [notification userInfo];

FBSessionState sessionState = [[userInfo objectForKey:@"state"] integerValue];
NSError *error = [userInfo objectForKey:@"error"];
// Usually, the only interesting states are the opened session, the closed session and the failed login.
if (!error) {
    // In case that there's not any error, then check if the session opened or closed.
    if (sessionState == FBSessionStateOpen) {

        [FBRequestConnection startWithGraphPath:@"me"
                                     parameters:@{@"fields": @"first_name, last_name, picture.type(normal), email"}
                                     HTTPMethod:@"GET"
                              completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                                  if (!error) {
                                      // Set the use full name.
                                      NSLog(@"first name and last name is %@",[NSString stringWithFormat:@"%@ %@",
                                                                               [result objectForKey:@"first_name"],
                                                                               [result objectForKey:@"last_name"]
                                                                               ]);
                                      NSString *firstName = [NSString stringWithFormat:@"%@",[result objectForKey:@"first_name"]];
                                      NSString *middleName = [NSString stringWithFormat:@"%@",[result objectForKey:@"middle_name"]];
                                      NSString *lastName = [NSString stringWithFormat:@"%@",[result objectForKey:@"last_name"]];
                                      NSString *email = [NSString stringWithFormat:@"%@",[result objectForKey:@"email"]];
                                      // Set the e-mail address.
                                      NSLog(@"email is %@", [result objectForKey:@"email"]);


                                  }
                                  else{


                                      NSLog(@"%@", [error localizedDescription]);
                                  }
                              }];


        // [self.btnToggleLoginState setTitle:@"Logout" forState:UIControlStateNormal];

    }
    else if (sessionState == FBSessionStateClosed || sessionState == FBSessionStateClosedLoginFailed){
        // A session was closed or the login was failed. Update the UI accordingly.
        //  [self.btnToggleLoginState setTitle:@"Login" forState:UIControlStateNormal];
        // self.lblStatus.text = @"You are not logged in.";



    }
  }
}
person Shruti    schedule 06.04.2015