iOS - 接入Facebook登录功能

官方指导

iOS 版 Facebook 登录 — 快速入门

  • 根据官方指导基本上可以实现基本的登录登出功能。
  1. Facebook注册账号
  2. Facebook开发者账号的注册,按照文章提示一般都可以很顺利的进行。
  3. 添加应用和设置bundleID,测试的APP中的bundleID必须保持一致。
  4. cocoapods添加facebook的SDK包
    //在podfile文件中加入以下文字
    pod 'FBSDKLoginKit'
  1. 配置项目
    使用包含应用数据的 XML 代码片段配置信息属性列表文件(info.plist)。
    右键点击 info.plist,然后选择 作为源代码打开。
    将下列 XML 代码片段复制并粘贴到文件正文中 (...)。
CFBundleURLTypes   CFBundleURLSchemes  fb349120409279673    FacebookAppID 349120409279673 FacebookDisplayName Lily Lee's first app

如要使用任何 Facebook 对话框(例如,登录、分享、应用邀请等),以便从您的应用切换至 Facebook 应用,则您应用程序的 info.plist 还必须包含:

LSApplicationQueriesSchemes  fbapi fb-messenger-share-api fbauth2 fbshareextension 
  1. 连接应用委托
    将下列代码添加到 AppDelegate 类中。这会在应用启动时初始化 SDK,并在您执行登录或分享操作时,让 SDK 处理通过原生 Facebook 应用获得的结果。
//  AppDelegate.m
#import 

- (BOOL)application:(UIApplication *)application 
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  
  [[FBSDKApplicationDelegate sharedInstance] application:application
    didFinishLaunchingWithOptions:launchOptions];
  // Add any custom logic here.
  return YES;
}

- (BOOL)application:(UIApplication *)application 
            openURL:(NSURL *)url 
            options:(NSDictionary *)options {

  BOOL handled = [[FBSDKApplicationDelegate sharedInstance] application:application
    openURL:url
    sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]
    annotation:options[UIApplicationOpenURLOptionsAnnotationKey]
  ];
  // Add any custom logic here.
  return handled;
}    
  1. 将 Facebook 登录功能添加到代码中

demo


#import "ViewController.h"
#import 
#import 

@interface ViewController ()
@property(nonatomic,strong)FBSDKProfilePictureView *profilePictureView;
@property(nonatomic,strong)UITextView *text;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    FBSDKLoginButton *loginButton = [[FBSDKLoginButton alloc] init];
    // Optional: Place the button in the center of your view.
    loginButton.center = self.view.center;
    [self.view addSubview:loginButton];

    [FBSDKProfile enableUpdatesOnAccessTokenChange:YES];
    [[NSNotificationCenter defaultCenter] addObserverForName:FBSDKProfileDidChangeNotification
                                                      object:nil
                                                       queue:[NSOperationQueue mainQueue]
                                                  usingBlock:
     ^(NSNotification *notification) {
         
         if ([FBSDKProfile currentProfile]) {
             // Update for new user profile
             //获取用户的头像
             FBSDKProfilePictureView *profilePictureView = [[FBSDKProfilePictureView alloc] init];
             self.profilePictureView = profilePictureView;
             self.profilePictureView.frame = CGRectMake([UIScreen mainScreen].bounds.size.width / 2 - 50,100,100,100);
             self.profilePictureView.profileID = [[FBSDKAccessToken currentAccessToken] userID];
             [self.view addSubview:self.profilePictureView];
             
             //获取当前用户名
             [FBSDKProfile loadCurrentProfileWithCompletion:
              ^(FBSDKProfile *profile, NSError *error) {
                  if (profile) {
                      UITextView *text = [[UITextView alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width / 2 - 50, 220, 100, 50)];
                      self.text = text;
                      [self.view addSubview:self.text];
                      text.text = profile.firstName;
                      text.textAlignment = NSTextAlignmentCenter;
                  }
              }];
             
         }
         if (![FBSDKProfile currentProfile]) {
             //当用户退出登录的时候,自动去除头像和用户名两个控件
             [self.profilePictureView removeFromSuperview];
             [self.text removeFromSuperview];
         }
     }];
}

// Once the button is clicked, show the login dialog
-(void)loginButtonClicked
{
    FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
    [login
     logInWithReadPermissions: @[@"public_profile"]
     fromViewController:self
     handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
         if (error) {
             NSLog(@"Process error");
         } else if (result.isCancelled) {
             NSLog(@"Cancelled");
         } else {
             NSLog(@"Logged in");
         }
     }];
}

@end

你可能感兴趣的:(iOS - 接入Facebook登录功能)