how to retrieve friend's birthday using Facebook SDK in ios?

后端 未结 5 1300
难免孤独
难免孤独 2020-12-30 17:33

I have implemented FBFriendPickerDelegate in my .h file and using the following delegate to retrieve information:

-(BOOL)friendPickerController:         


        
5条回答
  •  忘掉有多难
    2020-12-30 17:44

    Here is all of the code you need in one happy place. I had to read tons of post to get this far. Its a straight copy and paste code. Rate it up if it helps.

    In your *.m file

    #import 
    
    /******************
     FACEBOOK
     ******************/
    
    - (IBAction) facebookActionBtn
    {
    
        // Open session with public_profile (required) and user_birthday read permissions
        [FBSession openActiveSessionWithReadPermissions:@[@"public_profile", @"user_friends"]
                                           allowLoginUI:YES
                                      completionHandler:
         ^(FBSession *session, FBSessionState state, NSError *error) {
             __block NSString *alertText;
             __block NSString *alertTitle;
             if (!error){
                 // If the session was opened successfully
                 if (state == FBSessionStateOpen)
                 {
                     // Your code here
                     NSLog(@"all good ..");
    
                     [self getUserFriendsBDays];
    
                 }
                 else
                 {
                     // There was an error, handle it
                     if ([FBErrorUtility shouldNotifyUserForError:error] == YES)
                     {
    
                         alertTitle = @"Something went wrong";
                         alertText = [FBErrorUtility userMessageForError:error];
                         [[[UIAlertView alloc] initWithTitle:alertTitle
                                                     message:alertText
                                                    delegate:self
                                           cancelButtonTitle:@"OK!"
                                           otherButtonTitles:nil] show];
    
                     } else {
                         // If the user cancelled login
                         if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryUserCancelled)
                         {
                             alertTitle = @"Login cancelled";
                             alertText = @"Your friends birthday will not be entered in our calendar because you didn't grant the permission.";
                             [[[UIAlertView alloc] initWithTitle:alertTitle
                                                         message:alertText
                                                        delegate:self
                                               cancelButtonTitle:@"OK!"
                                               otherButtonTitles:nil] show];
    
                         } else {
                             NSLog(@"error 2 ..");
                         }
                     }
                 }
             }
         }];
    
    }
    
    - (IBAction) getUserFriendsBDays
    {
    
        NSLog(@"promptUserForFriendsBDays ...");
    
    
    
    
        NSString *fbAccessToken = [[[FBSession activeSession] accessTokenData] accessToken];
        //NSLog(@"permissions::%@ ... fbAccessToken: %@ ...",FBSession.activeSession.permissions, fbAccessToken);
    
    
        NSString *request = [NSString stringWithFormat:@"https://graph.facebook.com/me/friends?fields=id,name,birthday&access_token=%@",fbAccessToken];
        NSURL *URL = [NSURL URLWithString:request];
    
        NSError *error;
        NSString *FBOutPutStr = [NSString stringWithContentsOfURL:URL encoding:NSUTF8StringEncoding error:&error];
        //NSLog(@"FBOutPutStr: %@ ...", FBOutPutStr);
    
    
        // Convert to JSON object:
        NSDictionary* jsonObject = [NSJSONSerialization JSONObjectWithData:[FBOutPutStr dataUsingEncoding:NSUTF8StringEncoding] options:0 error:NULL];
        //NSLog(@"jsonObject=%@ ...", jsonObject);
    
        NSArray* dataField = [[NSArray alloc] initWithArray:[jsonObject objectForKey:@"data"]];
        NSLog(@"dataField=%@ ...", dataField);
    
    
        // Extract values:
        NSArray *userIDArray = [dataField valueForKey:@"id"];
        NSArray *userNameArray = [dataField valueForKey:@"name"];
        NSArray *userBdayArray = [dataField valueForKey:@"birthday"];
    
        NSLog(@"userIDArray=%d ... userNameArray=%d ... userBdayArray=%d ...", [userIDArray count],[userNameArray count],[userBdayArray count]);
    
    }
    

提交回复
热议问题