2013-05-25 38 views
12

Tôi mới phát triển iPhone. Tôi đang làm một ứng dụng mà người dùng có 2 thông tin đăng nhập thông qua Facebook, khi anh ấy gửi thông tin đăng nhập của mình và nhấp vào nút Đăng nhập, tôi phải tìm chi tiết như Tên, email và giới tính từ Facebook và sau khi tìm nạp người dùng phải được chuyển hướng đến trang Đăng ký của ứng dụng có đầy đủ chi tiết và tôi cần ứng dụng này tương thích với iPhone 4,4 và 5.Tìm nạp chi tiết người dùng từ Facebook trong iOS

Tôi đã cố gắng thực hiện việc này bằng API biểu đồ Facebook nhưng không thể tải xuống, vì vậy bất cứ ai cũng có thể giúp tôi.

Cảm ơn trước.

+3

Chào mừng bạn đến với SO. Bạn cần đăng những gì bạn đã thử. Chúng tôi sẽ cố gắng hỗ trợ bạn, nhưng chỉ sau khi bạn đã cho thấy rằng bạn đã dành ít nhất một số nỗ lực vào đó. –

Trả lời

1
1

Sử dụng API FBConnect cho lấy information.its người dùng dễ sử dụng

Fbconnect API

Hy vọng nó sẽ làm việc cho bạn :)

1

bạn có để sử dụng đường dẫn biểu đồ để nhận thông tin người dùng.

-(void)getEmailId 
{ 
[facebook requestWithGraphPath:@"me" andDelegate:self]; 
} 

- (void)openSession 
{ 
if (internetActive) { 
    NSArray *permissions=[NSArray arrayWithObjects:@"read_stream",@"email",nil]; 

    [FBSession openActiveSessionWithReadPermissions:permissions allowLoginUI:YES completionHandler: 
    ^(FBSession *session, 
    FBSessionState state, NSError *error) { 
    [self sessionStateChanged:session state:state error:error]; 
    }]; 
}else 
{ 
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"" message:@"Internet Not Connected" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; 
    [alert show]; 
} 
} 

#pragma mark- request delegate methods 

- (void)request:(FBRequest *)request didLoad:(id)result 
{ 
    NSLog(@"request did load successfully...."); 

// __block NSDictionary *dictionary=[[NSDictionary alloc] init]; 

if ([result isKindOfClass:[NSDictionary class]]) { 
    NSDictionary* json = result; 

    NSLog(@"email id is %@",[json valueForKey:@"email"]); 
    NSLog(@"json is %@",json); 

    [[NSUserDefaults standardUserDefaults] setValue:[json valueForKey:@"email"] forKey:@"fbemail"]; 
    [self.viewController login:YES]; 
} 
} 

- (void)request:(FBRequest *)request didFailWithError:(NSError *)error 
{ 
UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"" message:@"Server not responding.." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; 

[alertView show]; 

[self fblogout]; 
[self showLoginView]; 
NSLog(@"request did fail with error"); 
} 


- (void)sessionStateChanged:(FBSession *)session 
         state:(FBSessionState) state 
         error:(NSError *)error 
{ 
switch (state) { 
    case FBSessionStateOpen: { 
     //state open action 
    } 

    // Initiate a Facebook instance 
    facebook = [[Facebook alloc] 
        initWithAppId:FBSession.activeSession.appID 
        andDelegate:nil]; 

    // Store the Facebook session information 
    facebook.accessToken = FBSession.activeSession.accessToken; 
    facebook.expirationDate = FBSession.activeSession.expirationDate; 

    if (![[NSUserDefaults standardUserDefaults] valueForKey:@"fbemail"]) { 
    [MBProgressHUD showHUDAddedTo:self.viewController.view animated:YES]; 
    [self getEmailId]; 
    } 

    break; 
    case FBSessionStateClosed: 
    case FBSessionStateClosedLoginFailed: 
    // Once the user has logged in, we want them to 
    // be looking at the root view. 
    [self.navController popToRootViewControllerAnimated:NO]; 

    [FBSession.activeSession closeAndClearTokenInformation]; 
    facebook = nil; 

    [self showLoginView]; 
    break; 
    default: 
    break; 
} 

[[NSNotificationCenter defaultCenter] 
    postNotificationName:FBSessionStateChangedNotification 
    object:session]; 

if (error) { 
    UIAlertView *alertView = [[UIAlertView alloc] 
          initWithTitle:@"Error" 
          message:error.localizedDescription 
          delegate:nil 
          cancelButtonTitle:@"OK" 
          otherButtonTitles:nil]; 
    [alertView show]; 
} 
} 
18

Bạn có thể làm điều này bằng cách sử dụng đoạn mã sau:

[FBSession openActiveSessionWithReadPermissions:@[@"email",@"user_location",@"user_birthday",@"user_hometown"] 
            allowLoginUI:YES 
           completionHandler:^(FBSession *session, FBSessionState state, NSError *error) { 

            switch (state) { 
             case FBSessionStateOpen: 
              [[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) { 
               if (error) { 
               NSLog(@"error:%@",error);            
               } 
               else 
               {             
                // retrive user's details at here as shown below 
                NSLog(@"FB user first name:%@",user.first_name); 
                NSLog(@"FB user last name:%@",user.last_name); 
                NSLog(@"FB user birthday:%@",user.birthday); 
                NSLog(@"FB user location:%@",user.location); 
                NSLog(@"FB user username:%@",user.username); 
                NSLog(@"FB user gender:%@",[user objectForKey:@"gender"]); 
                NSLog(@"email id:%@",[user objectForKey:@"email"]); 
                NSLog(@"location:%@", [NSString stringWithFormat:@"Location: %@\n\n", 
                     user.location[@"name"]]); 

               } 
              }]; 
              break; 

             case FBSessionStateClosed: 
             case FBSessionStateClosedLoginFailed: 
              [FBSession.activeSession closeAndClearTokenInformation]; 
              break; 

             default: 
              break; 
            } 

           } ]; 

và đừng quên để nhập khẩu FacebookSDK/FacebookSDK.h trong mã của bạn.

EDIT: Cập nhật cho Facebook SDK v4 (23 Tháng Tư 2015)

Bây giờ, Faceboook đã phát hành SDK mới với những thay đổi lớn. Trong đó lớp FBSession không được chấp nhận. Vì vậy, tất cả người dùng được đề xuất di chuyển sang sdk và API mới.

Dưới đây tôi đã đề cập, làm thế nào chúng ta có thể nhận được thông tin chi tiết người dùng thông qua Facebook SDK v4:

if ([FBSDKAccessToken currentAccessToken]) { 
[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:nil] 
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) { 
    if (!error) { 
    NSLog(@”fetched user:%@”, result); 
    } 
}]; 
} 

Nhưng trước khi lấy chi tiết người dùng, chúng ta phải tích hợp đăng nhập Facebook mới trong mã của chúng tôi như được mô tả trong Documentation here.

Here là Changelog dành cho SDK phiên bản 4. Tôi đề nghị đi qua nó để được cập nhật.

+0

cách nhận số điện thoại của người dùng? –

+0

nó luôn đi vào FBSessionStateClosedLoginFailed ... Tại sao ...? –

+0

@RamaniAshish: Có thể do sự cố về quyền. Để hoàn thành tích hợp Facebook, chúng tôi phải có một ứng dụng đang hoạt động trong nhà phát triển FB. Khi chúng tôi cố gắng sử dụng chi tiết tài khoản người dùng, anh ấy phải cấp quyền truy cập vào chi tiết của anh ấy/cô ấy. đảm bảo tất cả những điều này đang xảy ra một cách hoàn hảo. Điều này sẽ làm việc như nó làm việc cho nhiều người khác bao gồm cả tôi :) – astuter

1
FBRequest *request = [FBRequest requestForMe]; 
[request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) { 
    if (!error) { 
     // handle successful response 
    } else if ([error.userInfo[FBErrorParsedJSONResponseKey][@"body"][@"error"][@"type"] isEqualToString:@"OAuthException"]) { // Since the request failed, we can check if it was due to an invalid session 
     NSLog(@"The facebook session was invalidated"); 
     [self logoutButtonTouchHandler:nil]; 
    } else { 
     NSLog(@"Some other error: %@", error); 
    } 
}]; 
3

Thêm thông tin.tập tin plist:

enter image description here

- (IBAction)btn_fb:(id)sender 
    { 
     if (!FBSession.activeSession.isOpen) 
      { 
       NSArray *_fbPermissions = @[@"email",@"publish_actions",@"public_profile",@"user_hometown",@"user_birthday",@"user_about_me",@"user_friends",@"user_photos",]; 

     [FBSession openActiveSessionWithReadPermissions:_fbPermissions allowLoginUI:YES completionHandler:^(FBSession *session,FBSessionState state, NSError *error) 
     { 
      if (error) 
      { 
       UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
       [alertView show]; 
      } 
      else if(session.isOpen) 
      { 
       [self btn_fb:sender]; 
      } 


     }]; 


     return; 
    } 


    [FBRequestConnection startWithGraphPath:@"me" parameters:[NSDictionary dictionaryWithObject:@"cover,picture.type(large),id,name,first_name,last_name,gender,birthday,email,location,hometown,bio,photos" forKey:@"fields"] HTTPMethod:@"GET" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) 
    { 
     { 
      if (!error) 
      { 
       if ([result isKindOfClass:[NSDictionary class]]) 
       { 
        NSLog(@"%@",result); 

       } 
      } 
     } 
    }]; 
} 
0

Facebook đã hiện cập nhật phiên bản SDK của họ để 4.x. Để tìm nạp thông tin tiểu sử của người dùng, bạn sẽ cần gọi rõ ràng biểu đồ API sau khi đăng nhập thành công (yêu cầu quyền bắt buộc).

FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] 
    initWithGraphPath:@"/me" 
      parameters:@{ @"fields": @"id,name,email"} 
      HTTPMethod:@"GET"]; 
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) { 
    // Insert your code here 
}]; 
Các vấn đề liên quan