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 代码片段复制并粘贴到文件正文中 (<dict>...</dict>)。
<key>CFBundleURLTypes</key> <array> <dict> <key>CFBundleURLSchemes</key> <array> <string>fb349120409279673</string> </array> </dict> </array> <key>FacebookAppID</key> <string>349120409279673</string> <key>FacebookDisplayName</key> <string>Lily Lee's first app</string>

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

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

- (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<UIApplicationOpenURLOptionsKey,id> *)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 <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKLoginKit/FBSDKLoginKit.h>

@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
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 13,874评论 1 32
  • 不行就打电话问,一定要打电话问 重要的事情打电话问 对,重要的事情打电话问
    路平028阅读 3,251评论 0 0
  • 文/陌宇轩 (黑龙江) 当我倾身喂哺你的第一口奶水 我听见了 你清脆的咕咚 咕咚 那声音打破了寂静的空间 一时间...
    小哲小诗阅读 1,229评论 0 0
  • 今天晚上,我吃完饭帮妈妈刷碗筷,我突然发现厨房里有一根萝卜长出了嫩绿的芽。我突发奇想,我要是把它种在花盆里...
    赵泽烁阅读 1,607评论 0 0
  • 1、一大早被梦惊醒了,竟然梦到了初中生物老师!我在装满课本的桌洞里寻找着课本…… 因为是初三要复习初中所有的教材,...
    师者之写道阅读 3,379评论 1 2

友情链接更多精彩内容