Demo address: http://download.csdn.net/download/ioscircleandcircle/9897852
A facebook access and login official document
https://developers.facebook.com/docs/facebook-login/ios
Facebook Login for iOS - Quick Start
When logging into your app with Facebook, the user can grant permissions to the app so that you can retrieve information or perform actions on Facebook as the user.
The following steps are used to add Facebook Login to your iOS Objective-C project. To add Facebook Login to your Swift project, see Facebook Login in Swift - Getting Started .
2. Set up the development environment t
Before logging in with Facebook for iOS, set up your development environment.
2a. Download the Facebook SDK for iOS
(Note, this is the Objective-C version of the SDK. For Swift, see Facebook Login for Swift - Getting Started .)
2b. Add the login kit to the Xcode project
- Open the app's Xcode project.
- 如果您的项目中没有 Frameworks(框架)组,请创建一个。
- 打开
~/Documents/FacebookSDK
。 - 将
FBSDKLoginKit
拖放至项目导航器内的Frameworks
。 - 在项目中打开 Xcode 的 Build Settings(构建设置)选项卡。
- 将
~/Documents/FacebookSDK
添加到项目的 Framework Search Paths(框架搜索路径)。
您使用 Facebook SDK 时,如果未禁用事件自动记录功能,应用事件将被自动记录和收集,用于 Facebook 分析。关于哪些信息会被收集以及如何禁用事件自动记录的详细信息,请参阅应用事件自动记录。
4.配置 info.plist
- 右键点击
.plist
文件,然后选择“Open As Source Code”(作为源代码打开)。 - 将下列 XML 代码片段复制并粘贴到文件正文中 (
<dict>...</dict>
)。 - 如果您使用任何 Facebook 对话框(例如:登录、分享、应用邀请等),以便从您的应用切换至 Facebook 应用,则您应用的
.plist
还必须处理以下代码。
5.连接应用委托
AppDelegate
类关联到
FBSDKApplicationDelegate
对象。要实现这一操作,请将以下代码添加到
AppDelegate.m
文件。
// AppDelegate.m #import <FBSDKCoreKit/FBSDKCoreKit.h> - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [[FBSDKApplicationDelegate sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions]; // 在此添加任意自定义逻辑。 return YES; } - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { BOOL handled = [[FBSDKApplicationDelegate sharedInstance] application:application openURL:url sourceApplication:sourceApplication annotation:annotation ]; // 在此添加任意自定义逻辑。 return handled; }
6.将 Facebook 登录功能添加到代码中
在您的 iOS 应用中添加 Facebook 登录按钮。
6a.将 Facebook 登录功能添加到代码中
// 将下列代码添加到文件标头中,例如:在 ViewController.m 中 // 置于 #import "ViewController.h" 之后 #import <FBSDKCoreKit/FBSDKCoreKit.h> #import <FBSDKLoginKit/FBSDKLoginKit.h> // 将以下代码添加到正文 @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; FBSDKLoginButton *loginButton = [[FBSDKLoginButton alloc] init]; //或者:将按钮放在视图的中间位置。 loginButton.center = self.view.center; [self.view addSubview:loginButton]; } @end
6b.检查当前登录状态
[FBSDKAccessToken currentAccessToken]
代表登录应用的每位用户。
FBSDKLoginManager
将为您设置此口令,且在设置
currentAccessToken
时,还会自动将口令写入 keychain 缓存。
6c.请求权限
FBSDKLoginButton
),可使用
readPermissions
属性请求附加的读取权限。
7.后续步骤
二 facebooK自定义登录按钮
https://developers.facebook.com/docs/facebook-login/ios/advanced
//自定义登陆按钮 - (void)customLogin { UIButton *myLoginButton=[UIButton buttonWithType:UIButtonTypeCustom]; myLoginButton.backgroundColor=[UIColor darkGrayColor]; myLoginButton.frame=CGRectMake(0,60,180,40); // myLoginButton.center = self.view.center; [myLoginButton setTitle: @"My Login Button" forState: UIControlStateNormal]; // Handle clicks on the button [myLoginButton addTarget:self action:@selector(loginButtonClicked) forControlEvents:UIControlEventTouchUpInside]; // Add the button to the view [self.view addSubview:myLoginButton]; } // @[@"public_profile", @"email", @"user_friends"]; 您的应用即可请求用户的电子邮箱和好友列表。 -(void)loginButtonClicked { FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init]; [login logOut];////这个一定要写,不然会出现换一个帐号就无法获取信息的错误 [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"); [self getUserInfoWithResult:result]; [self getUserListWithResult:result]; }
三 facebook可以获取的权限
https://developers.facebook.com/docs/facebook-login/permissions
四 facebook获取用户信息
//获取用户信息 picture用户头像 - (void)getUserInfoWithResult:(FBSDKLoginManagerLoginResult *)result { NSDictionary*params= @{@"fields":@"id,name,email,age_range,first_name,last_name,link,gender,locale,picture,timezone,updated_time,verified"}; FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:result.token.userID parameters:params HTTPMethod:@"GET"]; [request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) { NSLog(@"%@",result); /* { "age_range" = { min = 21; }; "first_name" = "\U6dd1\U5a1f"; gender = female; id = 320561731689112; "last_name" = "\U6f58"; link = "https://www.facebook.com/app_scoped_user_id/320561731689112/"; locale = "zh_CN"; name = "\U6f58\U6dd1\U5a1f"; picture = { data = { "is_silhouette" = 0; url = "https://fb-s-c-a.akamaihd.net/h-ak-fbx/v/t1.0-1/p50x50/18157158_290358084709477_3057447496862917877_n.jpg?oh=01ba6b3a5190122f3959a3f4ed553ae8&oe=5A0ADBF5&__gda__=1509731522_7a226b0977470e13b2611f970b6e2719"; }; }; timezone = 8; "updated_time" = "2017-04-29T07:54:31+0000"; verified = 1; } */ }]; }
五 facebook获取朋友列表
//获取朋友列表 - (void)getUserListWithResult:(FBSDKLoginManagerLoginResult *)result { NSString *token = [FBSDKAccessToken currentAccessToken].tokenString; NSLog(@"%@",token); FBSDKGraphRequest *requestT = [[FBSDKGraphRequest alloc] initWithGraphPath:[NSString stringWithFormat:@"%@/friends",result.token.userID] parameters:@{@"fields":@"uid"} HTTPMethod:@"GET"]; [requestT startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) { NSLog(@"friends:%@",result); /* { data = ( ); summary = { "total_count" = 314; }; } */ }]; }
Edit: psj