これが、1つの幸せな場所に必要なすべてのコードです。私はこれを達成するためにたくさんの投稿を読まなければなりませんでした。そのストレートコピーアンドペーストコード。役に立ったら評価してください。
*.mファイル内
#import <FacebookSDK/FacebookSDK.h>
/******************
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]);
}