ログインボタンのクリックで次のコードを使用します。
FBSDKLoginManager *loginManager = [[FBSDKLoginManager alloc] init];
[loginManager setLoginBehavior:FBSDKLoginBehaviorSystemAccount];
[loginManager logInWithReadPermissions:@[@"public_profile", @"email", @"user_friends"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
{
if (error)
{
[MBProgressHUD hideHUDForView:self.view animated:YES];
[loginManager logOut];
}
else if (result.isCancelled)
{
[MBProgressHUD hideHUDForView:self.view animated:YES];
[loginManager logOut];
}
else
{
if ([result.grantedPermissions containsObject:@"publish_actions"])
{
NSTimeInterval addTimeInterval = 60*60*24*365*50;
NSDate *expireDate = [[NSDate date] dateByAddingTimeInterval:addTimeInterval];
NSDate *refreshDate = [[NSDate date] dateByAddingTimeInterval:addTimeInterval];
FBSDKAccessToken *newAccessToken = [[FBSDKAccessToken alloc] initWithTokenString:[[FBSDKAccessToken currentAccessToken] tokenString] permissions:nil declinedPermissions:nil appID:FACEBOOK_ID userID:[[FBSDKAccessToken currentAccessToken] userID] expirationDate:expireDate refreshDate:refreshDate];
[FBSDKAccessToken setCurrentAccessToken:newAccessToken];
[self FacebookUserInfo];
}
else
{
[loginManager logInWithPublishPermissions:@[@"publish_actions"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
{
if (error)
{
[MBProgressHUD hideHUDForView:self.view animated:YES];
[loginManager logOut];
}
else if (result.isCancelled)
{
[MBProgressHUD hideHUDForView:self.view animated:YES];
[loginManager logOut];
}
else
{
NSTimeInterval addTimeInterval = 60*60*24*365*50;
NSDate *expireDate = [[NSDate date] dateByAddingTimeInterval:addTimeInterval];
NSDate *refreshDate = [[NSDate date] dateByAddingTimeInterval:addTimeInterval];
FBSDKAccessToken *newAccessToken = [[FBSDKAccessToken alloc] initWithTokenString:[[FBSDKAccessToken currentAccessToken] tokenString] permissions:nil declinedPermissions:nil appID:FACEBOOK_ID userID:[[FBSDKAccessToken currentAccessToken] userID] expirationDate:expireDate refreshDate:refreshDate];
[FBSDKAccessToken setCurrentAccessToken:newAccessToken];
[self FacebookUserInfo];
}
}];
}
}
}];
Access Token を取得したら、次のメソッドを呼び出します。
- (void)FacebookUserInfo
{
[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields":@"name,email,first_name,last_name"}]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error)
{
if (!error)
{
NSDictionary *dict = @{@"id":result[@"id"],
@"name":result[@"first_name"],
@"email":result[@"email"]
};
[self saveData:dict];
}
else
{
}
}];
}
これはあなたにとって役立つかもしれません。