简体   繁体   English

iOS Facebook SDK - 检查权限

[英]iOS Facebook SDK - Check Permissions

I have a Facebook SSO working perfectly on my app, using last release of Facebook Objective-C SDK. 我使用Facebook Objective-C SDK的最新版本在我的应用程序上完美运行了Facebook SSO。 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 如果用户之前给我的话,我不需要问这个许可,所以,我想,在Facebook SDK中应该有一个方法

-(BOOL) checkPermission:(NSString*) permission;

so I can use it like this: 所以我可以像这样使用它:

if( [facebook checkPermission:@"email"] ) {

Is there a way to do this? 有没有办法做到这一点?

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 : 以下是在HelloFacebookSample中完成的操作:

if ([FBSession.activeSession.permissions indexOfObject:@"publish_actions"] == NSNotFound) {
    // permission does not exist
} else {
    // permission exists
}

Just replace "publish_actions" with "email". 只需将“publish_actions”替换为“email”即可。

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 SDK不提供检查特定权限的直接方法,但您可以通过检查Graph APIuser对象的 permissions连接来检查用户是否授予了应用程序permissions

GET https://graph.facebook.com/me/permissions

Same can be achieved with FQL query on permissions table 使用permissions上的FQL查询可以实现相同

SELECT email FROM permissions WHERE uid = me()

IMPORTANT: This seems to be true for an older version of the Facebook SDK (for example 3.9.0). 重要提示:对于较旧版本的Facebook SDK(例如3.9.0),这似乎是正确的。 In 3.15.0 it doesn't work this way anymore. 在3.15.0中,它不再以这种方式工作。 You should use [session.permissions] as Raphaël Agneau says in his answer. 你应该使用[session.permissions]因为RaphaëlAgneau在他的回答中说。

You have to use the following method, because [FBSession activeSession].permissions seems to return the permissions you requested, not the real ones. 您必须使用以下方法,因为[FBSession activeSession].permissions似乎返回您请求的权限,而不是真实的权限。

[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 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
}];

Here's my code for FBSDK 4.2.0 for checking permissions. 这是我的FBSDK 4.2.0代码,用于检查权限。 The string that's passed in is the name of the permission, eg "publish_actions" 传入的字符串是权限的名称,例如“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;
}
}

For the SDK 4+ you have these 2 ways of getting the permissions: 对于SDK 4+,您有以下两种获取权限的方法:

[[FBSDKAccessToken currentAccessToken] hasGranted:@"user_photos"]

or 要么

[[[FBSDKAccessToken currentAccessToken] permissions] containsObject:@"user_photos"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM