Skip to content

Instantly share code, notes, and snippets.

@evgeniyd
Last active December 21, 2015 13:44
Show Gist options
  • Save evgeniyd/64e782770c241e92b5e6 to your computer and use it in GitHub Desktop.
Save evgeniyd/64e782770c241e92b5e6 to your computer and use it in GitHub Desktop.
Basic actions with FBSDK 4.x
//
// Login via FBSDK
//
- (void)connectFacebookAccountWithCompletionHandler:(void (^)(NSError *))completionHandler
{
NSArray *readPermissions = @[@"public_profile", @"user_friends", @"email", @"user_likes"];
if (![FBSDKAccessToken currentAccessToken]) {
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logInWithReadPermissions:readPermissions
fromViewController:nil // TODO: provide a VC
handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
[self fbLoginManagerLoginResult:result
didChangeWithError:error];
if (completionHandler) {
completionHandler(error);
}
}];
}
else {
if (completionHandler) {
completionHandler(nil);
}
}
}
//
// Process FB Login response
//
- (void)fbLoginManagerLoginResult:(FBSDKLoginManagerLoginResult *)result didChangeWithError:(NSError *)error
{
if (error) {
CLS_LOG(@"FB auth error %@", [error detailedInfo]);
// Notify about error
}
else if (result.isCancelled) {
CLS_LOG(@"FB auth cancelled");
[self logoutFacebook];
}
else {
// check if specific permissions missing
if (![result.grantedPermissions containsObject:@"user_likes"]) {
CLS_LOG(@"\"user_likes\" FB permissions are missing");
}
CLS_LOG(@"login");
// do login with your server, if needed
}
}
//
// Logout FB
//
- (void)logoutFacebook
{
if ([FBSDKAccessToken currentAccessToken]) {
[[FBSDKLoginManager new] logOut];
}
}
//
// get FB Profile
//
if ([FBSDKAccessToken currentAccessToken]) {
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:@"me"
parameters:nil];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSDictionary* user = (NSDictionary *)result;
// [user objectForKey:@"name"]
// [user objectForKey:@"id"]
}
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment