Help us understand the problem. What is going on with this article?

FacebookSDK foriOS その2

More than 5 years have passed since last update.

FacebookSDK for iOSを使ってみたメモ2です。

ログインしたら、次はパーミッションの設定です。

https://developers.facebook.com/docs/facebook-login/permissions

何が必要かは、こちらのリファレンスから必要最小限の権限を選択する必要がありますが、選択してしまえば簡単です。

FBLoginView *loginView = 
    [[FBLoginView alloc] initWithReadPermissions:@[@"basic_info", @"email", @"user_likes"]];

とか

FBLoginView *loginView = [[FBLoginView alloc] init];
loginView.readPermissions = @[@"basic_info", @"email", @"user_likes"];

のようにすればいいんですが、StoryBoardでLoginViewを設定している場合にはこれではうまくいきません。当然ですが。

なので、ちょっと長いですが、サンプルをそのまま使うと、

// We will request the user's public profile and the user's birthday
// These are the permissions we need:
NSArray *permissionsNeeded = @[@"basic_info", @"user_photos", @"user_about_me"];

// Request the permissions the user currently has
[FBRequestConnection startWithGraphPath:@"/me/permissions"
    completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                      if (!error){
                        // These are the current permissions the user has:
                        NSDictionary *currentPermissions= [(NSArray *)[result data] objectAtIndex:0];
                        // We will store here the missing permissions that we will have to request
                        NSMutableArray *requestPermissions = [[NSMutableArray alloc] initWithArray:@[]];

                        // Check if all the permissions we need are present in the user's current permissions
                        // If they are not present add them to the permissions to be requested
                        for (NSString *permission in permissionsNeeded){
                          if (![currentPermissions objectForKey:permission]){
                             [requestPermissions addObject:permission];
                          }
                        }

                        // If we have permissions to request
                        if ([requestPermissions count] > 0){
                        // Ask for the missing permissions
                        [FBSession.activeSession
                          requestNewReadPermissions:requestPermissions
                          completionHandler:^(FBSession *session, NSError *error) {
                            if (!error) {
                              // Permission granted
                              NSLog(@"new permissions %@", [FBSession.activeSession permissions]);
                              // We can request the user information
                              [self getData];
                            } else {
                              // An error occurred, we need to handle the error
                              // See: https://developers.facebook.com/docs/ios/errors
                            }
                          }];
                        } else {
                          // Permissions are present
                          // We can request the user information
                          [self getData];
                        }

                      } else {
                        // An error occurred, we need to handle the error
                        // See: https://developers.facebook.com/docs/ios/errors
                      }
              }];

のようにすればいいです。
#もちろん適時getDataとか、エラー処理とかをいれる必要はありますが。

このように、GraphAPIを使う場合には、基本的にFBRequestConnectionを介して、ブロック型に対してコールバックをもらう(厳密な表現じゃないとは思いますが)形で叩くようです。

ブロック構文のメリットはありますが、どうしてもインデントが深くなりがちだったり、同期・非同期を正しく扱う必要があったり、慣れが必要ですね。

まぁ、たぶんObjective-Cを使っている方であれば、すぐ慣れるのかもしれませんが・・・

さて、これでデータを取得する準備が完了しましたとさ。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした