2015-04-06 12 views
21

Nesne C ve iPhone Geliştirme ortamlarına oldukça yeni geliyorum.Kullanıcının kişisel bilgileri iOS'tan Facebook'a

Kullanıcı adı, E-posta ve profil Resmi almak için uygulamasında Facebook girişini uygulıyorum. Oturum açma Bölümünü başarıyla uyguladı ve kişinin adını ve Kullanıcı Kimliğini aldım.

Şimdi Facebook'un Kullanıcı E-posta ve Profil Resmini almak istiyorum.Ama herhangi bir Fikir alamıyorum. Facebook IOS SDK v4.0'ı kullanıyorum.

Kullanıcı kimliğindeyken, Kullanıcı Profili resmini ve E-posta kimliğini Facebook'tan nasıl alabilirim?

+0

onay bu .... http://stackoverflow.com/questions/29466795/getting-facebook-public-profile-of-a-person-in-ios-app/29466882#29466882 – Shruti

+0

http:// stackoverflow.com/questions/22111993/how-to-get-kullanıcı-bilgi-from-facebook-sdk-in-ios – tharif

+0

@Shruti Bu bağlantıyı zaten denedim Tamam ama ben sadece ad ve soyadı ve hiçbir hükmü var E-posta Kimliğini alma –

cevap

55

Kullanıcı e-posta kimliğini almak için, kayıt sırasında e-posta adresine e-posta gönderin.

FBSDKLoginButton *loginView = [[FBSDKLoginButton alloc] init]; 
loginView.readPermissions = @[@"email"]; 
loginView.frame = CGRectMake(100, 150, 100, 40); 
[self.view addSubview:loginView]; 

Sen GraphPath kullanarak Yeni SDK'deki kullanıcı e-posta kimliği alabilirsiniz.

[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:nil] 
     startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) { 

      if (!error) { 
       NSLog(@"fetched user:%@ and Email : %@", result,result[@"email"]); 
     } 
     }]; 
    } 

sonuç size tüm kullanıcı Ayrıntıları almak ve neden olacaktır oturum açan kullanıcı için [ "email" @] size e-posta almak istiyorum. bakınız

FBSDKProfilePictureView *profilePictureview = [[FBSDKProfilePictureView alloc]initWithFrame:_imageView.frame]; 
[profilePictureview setProfileID:result[@"id"]]; 
[self.view addSubview:profilePictureview]; 

: https://developers.facebook.com/docs/facebook-login/ios/v2.3#profile_picture_view

veya

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=normal",result[@"id"]]]; 
NSData *data = [NSData dataWithContentsOfURL:url]; 
_imageView.image = [UIImage imageWithData:data]; 

kullanabilir veya u da kullanıcı profili kimliği geçirerek profil resmi almak için FBSDKProfilePictureView kullanabilirsiniz

Profil resim elde etmek için Ayrıca,

parametrelerini geçirerek de alabilirsiniz
+1

merhaba dheeraj bunu denedim ama e-postamda null alıyorum. –

+0

Kayıt sırasında e-posta için izin istemelisiniz. –

+0

İşi bitirdim FBSDKLoginButton * loginButton = [[FBSDKLoginButton alloc] init]; loginButton.center = self.view.center; [self.view addSubview: loginButton]; self.loginButton.readPermissions = @ [@ "public_profile", @ "email"]; benim viewDidLoad içinde –

21

Bu dağınık cevap için üzgünüm, bu benim ilk cevabım. Kullanıcının Profile Picture uygulamasını kolayca almak için kullanıcının tüm profil bilgilerini ve FBSDKProfilePictureView sınıfını getirmek için FBSDK Grafik isteğini kullanabilirsiniz. Bu kod elle Facebook oturum açma kullanıcı arayüzü içindir.

Öncelikle, giriş işlemi başlatmak bu kodu koymak gerekir:

FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init]; 

[login logInWithReadPermissions:@[@"public_profile", @"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) { 

    if (error) 
    { 

    // There is an error here. 

    } 
    else 
    { 
     if(result.token) // This means if There is current access token. 
     {  
      // Token created successfully and you are ready to get profile info 
      [self getFacebookProfileInfo]; 
     }   
    } 
}]; 

Ve giriş başarılı olursa, kullanıcının genel profil almak için bu yöntemi uygulamak;

-(void)getFacebookProfileInfos { 

FBSDKGraphRequest *requestMe = [[FBSDKGraphRequest alloc]initWithGraphPath:@"me" parameters:nil]; 

FBSDKGraphRequestConnection *connection = [[FBSDKGraphRequestConnection alloc] init]; 

[connection addRequest:requestMe completionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) { 

     if(result) 
     {    
     if ([result objectForKey:@"email"]) { 

      NSLog(@"Email: %@",[result objectForKey:@"email"]); 

     } 
     if ([result objectForKey:@"first_name"]) { 

      NSLog(@"First Name : %@",[result objectForKey:@"first_name"]); 

     } 
     if ([result objectForKey:@"id"]) { 

      NSLog(@"User id : %@",[result objectForKey:@"id"]); 

     } 

     } 

}]; 

[connection start]; 

kullanıcının profil resminin mevcut giriş yapılmış alın:

[FBSDKProfile enableUpdatesOnAccessTokenChange:YES]; 
+0

facebookLoginButtonTouched, –

+0

olarak adlandırılmıyor Projemde özel Facebook Giriş düğmesini kullanıyorum. Yani "facebookLoginButtonTouched" özel giriş düğmem "içte" yöntemidir. Şimdi cevabımı düzenliyorum. –

+1

Bu konudaki en alakalı cevap. Messy'den uzak. Teşekkürler dostum! –

4
Hope This could Help You .. 

- (IBAction)Loginwithfacebookaction:(id)sender 
{ 


    FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init]; 
    [login logOut]; 

    [login logInWithReadPermissions:@[@"public_profile"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) { 
     if (error) 
     { 
      NSLog(@"Process error"); 
     } 
     else if (result.isCancelled) 
     { 
      NSLog(@"Cancelled"); 
     } 
     else 
     { 
      [self getFacebookProfileInfos]; 
     } 
    }]; 
} 

- (void)finishedWithAuth: (GTMOAuth2Authentication *)auth 
        error: (NSError *) error { 

    NSLog(@"Received error %@ and auth object %@",error, auth); 
    if (!error) 
    { 
     email =signIn.userEmail; 
     [[NSUserDefaults standardUserDefaults] setObject:email forKey:@"useremail"]; 
     NSLog(@"Received error and auth object %@",signIn.userEmail); 
     NSLog(@"Received error and auth object %@",signIn.userID); 
     if (auth.userEmail) 
     { 
      [[[GPPSignIn sharedInstance] plusService] executeQuery:[GTLQueryPlus queryForPeopleGetWithUserId:@"me"] completionHandler:^(GTLServiceTicket *ticket, GTLPlusPerson *person, NSError *error) 
      { 
       // this is for fetch profile image 
       NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",person.image.url]]; 
       NSLog(@"%@",url); 
       name= person.displayName; 
        [[NSUserDefaults standardUserDefaults] setObject:name forKey:@"userNameLogin"]; 
        [[NSUserDefaults standardUserDefaults] synchronize]; 
       NSLog(@"Name:%@",person.displayName); 
       [self callWebserviceToUploadImage]; 
      }]; 

     } 
    } 
} 

-(void)getFacebookProfileInfos { 

    FBSDKGraphRequest *requestMe = [[FBSDKGraphRequest alloc]initWithGraphPath:@"/me?fields=first_name, last_name, picture, email" parameters:nil]; 

    FBSDKGraphRequestConnection *connection = [[FBSDKGraphRequestConnection alloc] init]; 


    [connection addRequest:requestMe completionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) { 

     if(result) 
     { 
      if ([result objectForKey:@"email"]) { 
       email = [result objectForKey:@"email"]; 

       [[NSUserDefaults standardUserDefaults] setObject:email forKey:@"useremail"]; 

      } 
      if ([result objectForKey:@"first_name"]) { 

       NSLog(@"First Name : %@",[result objectForKey:@"first_name"]); 
       name = [result objectForKey:@"first_name"]; 
       [[NSUserDefaults standardUserDefaults] setObject:name forKey:@"userNameLogin"]; 

      } 
      if ([result objectForKey:@"id"]) 
      { 

       NSLog(@"User id : %@",[result objectForKey:@"id"]); 

      } 
     } 
     [self callfbloginwebservice]; 

    }]; 
    [connection start]; 

} 
1
#import <FBSDKCoreKit/FBSDKAccessToken.h> 
#import <FBSDKCoreKit/FBSDKGraphRequest.h> 

YourViewController.h

ekleyin:

FBSDKProfilePictureView *pictureView=[[FBSDKProfilePictureView alloc]init]; 

[pictureView setProfileID:@"user_id"]; 

[pictureView setPictureMode:FBSDKProfilePictureModeSquare]; 

[self.view addSubview:pictureView]; 

Sen viewDidLoad yöntemine ferahlatıcı kod eklemeniz gerekir

- (IBAction)loginAction:(id)sender { 

    // https://developers.facebook.com/docs/graph-api/reference/user 
    // https://developers.facebook.com/docs/ios/graph 


    if ([FBSDKAccessToken currentAccessToken]) { 
     [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"email,name,first_name,last_name"}] 
     startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) { 
      if (!error) { 
       NSLog(@"fetched user:%@", result); 
       // Here u can update u r UI like email name TextField 
      } 
     }]; 


    } 

} 
İlgili konular