LoginSignup
65
64

More than 5 years have passed since last update.

iOSで新しくなったFacebookの認証を試す

Last updated at Posted at 2015-04-01

追記2015/8/21

iOS9でFacebookの認証を利用する場合はいくつか設定が必要です
以下のFacebook開発者サイトをご覧ください。

設定内容は3つあります。

  1. 通信を行うサーバーをホワイトリストとしてInfo.plistに登録
  2. Enable bitcodeをNOにする
    • XcodeのBuild Settingsから設定できます
  3. Facebook SDKが利用するURLスキーマの種類をInfo.plistに登録
    • 使用するSDKのバージョンによって設定が異なります

Facebookアプリを作成

  • iOSを選択してFacebookアプリを新規に作成

設定

  • FacebookSDKがいくつかに分かれているので下記をXcodeに追加
    • FBSDKCoreKit.framework
    • FBSDKLoginKit.framework
    • FBSDKShareKit.framework
  • プロパティリストを編集
    • FacebookAppID
    • FacebookDisplayName
    • URL Schemes
  • Facebook側にBundle IDを登録

AppDelegateの設定

  • CoreKitのインポート
#import <FBSDKCoreKit/FBSDKCoreKit.h>
  • デリゲートメソッドの編集
- (void)applicationDidBecomeActive:(UIApplication *)application {
  [FBSDKAppEvents activateApp];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  return [[FBSDKApplicationDelegate sharedInstance] application:application
                                    didFinishLaunchingWithOptions:launchOptions];
}

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {
  return [[FBSDKApplicationDelegate sharedInstance] application:application
                                                         openURL:url
                                               sourceApplication:sourceApplication
                                                      annotation:annotation];
}

viewControllerの設定

  • CoreKitとLoginKitのインポート
#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKLoginKit/FBSDKLoginKit.h>
  • LoginButtonの追加
//viewControllerのviewDidLoadに追加
FBSDKLoginButton *loginButton = [[FBSDKLoginButton alloc] init];
loginButton.center = self.view.center;
[self.view addSubview:loginButton];

実行してみる

  • ログインボタンが出現
  • ログインできたっぽい

アクセストークンを確認

  • viewDidLoadを改修
  • FBSDKAccessTokenを利用
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    FBSDKLoginButton *loginButton = [[FBSDKLoginButton alloc] init];
    loginButton.center = self.view.center;
    [self.view addSubview:loginButton];

    //アクセストークンを取得
    FBSDKAccessToken *token = [FBSDKAccessToken currentAccessToken];
    if (token){
        NSDictionary *authData = @{@"id":token.userID,
                                   @"access_token":token.tokenString,
                                   @"expiration_date":token.expirationDate};
        NSLog(@"Facebook authdata:%@", authData);
    } else {
        NSLog(@"currentAccessToken is nil");
    }
}

パーミッションを設定する

  • viewDidLoadを編集
  • loginButtonのプロパティに設定しておくと必要なパーミッションを要求してくれる
FBSDKLoginButton *loginButton = [[FBSDKLoginButton alloc] init];
loginButton.center = self.view.center;

//パーミッションのリクエスト用にプロパティを設定
loginButton.readPermissions = @[@"public_profile", @"email", @"user_friends"];
[self.view addSubview:loginButton];

カスタムUIでチャレンジしてみる

  • Storyboardでボタンを追加し、タップ時の処理を用意する
  • FBSDKLoginManagerクラスを利用してログインAPIを呼び出す
//ログインボタンが押されたときの処理
- (IBAction)fbLogin:(id)sender {
    FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
    [login logInWithReadPermissions:@[@"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
        if (error) {
            NSLog(@"errror:%@", error);
            // Process error
        } else if (result.isCancelled) {
            NSLog(@"login is cancelled.");
            // Handle cancellations
        } else {
            // If you ask for multiple permissions at once, you
            // should check if specific permissions missing
            if ([result.grantedPermissions containsObject:@"email"]) {
                // Do work
                NSLog(@"login with read email permission succeeded.");
            }
        }
    }];
}
65
64
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
65
64