官方指导
- 根据官方指导基本上可以实现基本的登录登出功能。
- Facebook注册账号
- Facebook开发者账号的注册,按照文章提示一般都可以很顺利的进行。
- 添加应用和设置bundleID,测试的APP中的bundleID必须保持一致。
- cocoapods添加facebook的SDK包
//在podfile文件中加入以下文字
pod 'FBSDKLoginKit'
- 配置项目
使用包含应用数据的 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>
- 连接应用委托
将下列代码添加到 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;
}
- 将 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