Reputation: 1561
I have a Facebook SSO working perfectly on my app, using last release of Facebook Objective-C SDK. I need to ask an extra permission inside the app if user do "something". I don't need to ask that permission if user gave it to me before, so, I guess, in Facebook SDK there should be a method
-(BOOL) checkPermission:(NSString*) permission;
so I can use it like this:
if( [facebook checkPermission:@"email"] ) {
Is there a way to do this?
Upvotes: 8
Views: 10249
Reputation: 13459
For the SDK 4+ you have these 2 ways of getting the permissions:
[[FBSDKAccessToken currentAccessToken] hasGranted:@"user_photos"]
or
[[[FBSDKAccessToken currentAccessToken] permissions] containsObject:@"user_photos"]
Upvotes: 0
Reputation: 398
Here's my code for FBSDK 4.2.0 for checking permissions. The string that's passed in is the name of the permission, e.g. "publish_actions"
- (void) checkForPermission:(NSString *)permission granted:(void (^)(void))sBlock denied:(void (^)(void))fBlock {
if ([FBSDKAccessToken currentAccessToken]) {
[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me/permissions" parameters:nil] startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
BOOL hasPermission = NO;
if (!error) {
NSArray *permissions = [result objectForKey:@"data"];
for (NSDictionary *dict in permissions) {
if ([[dict objectForKey:@"permission"] isEqualToString:permission]) {
if ([[dict objectForKey:@"status"] isEqualToString:@"granted"]) {
hasPermission = YES;
}
}
}
}
if (hasPermission) {
(sBlock) ? sBlock() : sBlock;
} else {
(fBlock) ? fBlock() : fBlock;
}
}];
} else {
(fBlock) ? fBlock() : fBlock;
}
}
Upvotes: 0
Reputation: 11519
IMPORTANT: This seems to be true for an older version of the Facebook SDK (for example 3.9.0). In 3.15.0 it doesn't work this way anymore. You should use [session.permissions]
as Raphaël Agneau says in his answer.
You have to use the following method, because [FBSession activeSession].permissions
seems to return the permissions you requested, not the real ones.
[FBRequestConnection startWithGraphPath:@"/me/permissions"
completionHandler:^(FBRequestConnection *c, id result, NSError *error) {
if (!error) {
NSDictionary *permissions= [(NSArray *)[result data] objectAtIndex:0];
if (![permissions objectForKey:@"publish_actions"]) {
// Ok, continue with your logic
} else {
// Permission not found, maybe request it (see below)
}
} else {
// Treat error
}
}];
See here for more info:
https://developers.facebook.com/docs/facebook-login/ios/v2.0#permissions-checking
If the permission is not found you may want to request it this way:
[session requestNewPublishPermissions:PERMISSIONS_YOU_WANT
defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession* session, NSError* error) {
// Try again the /me/permissions above
}];
Upvotes: 0
Reputation: 508
This question is a bit old but you can now check what permissions the active session has without making a graph request. Here is how it's done in the HelloFacebookSample :
if ([FBSession.activeSession.permissions indexOfObject:@"publish_actions"] == NSNotFound) {
// permission does not exist
} else {
// permission exists
}
Just replace "publish_actions" with "email".
Upvotes: 22
Reputation: 25918
SDK not providing direct method for checking specific permissions but you can check if user granted permission to your application by checking permissions
connection of user
object in Graph API
GET https://graph.facebook.com/me/permissions
Same can be achieved with FQL query on permissions
table
SELECT email FROM permissions WHERE uid = me()
Upvotes: 7