Facebook登录(Login Button)
facebook提供了一个及其简单的登录方式:Login Button(以下简称fbButton):
这里就说一下如何使用fbButton:
1.先将facebook的sdk引入你的工程(可以去官网下载,然后拖拽或者使用Cocoapods引入)
2.去facebook创建你的app(https://developers.facebook.com/apps,官网有视频说明如何申请:http://developers.facebook.com/docs/getting-started/facebook-sdk-for-ios/3.1/)
3.创建好之后点击如下图所示按钮
点击Native iOS App这一栏,如图所示:
Bundle ID必须与你工程中.plist文件的Bundle identifier名称一样,App Store ID就是你app的itunes ID,记得下面勾选enabled。最后一个输入框可以不填写。
5.然后我们回到我们工程的.plist文件。添加下图所示信息
那串数字是你在facebook注册app时给你的id。
这样,准备工作就做好了。
然后添加一些代码:
首先,在appDelegate中,添加下面的代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{//...[FBLoginView class];return YES;
}
- (BOOL)application:(UIApplication *)applicationopenURL:(NSURL *)urlsourceApplication:(NSString *)sourceApplicationannotation:(id)annotation {// Call FBAppCall's handleOpenURL:sourceApplication to handle Facebook app responsesBOOL wasHandled = [FBAppCall handleOpenURL:url sourceApplication:sourceApplication];// You can add your app-specific url handling code here if neededreturn wasHandled;
}
然后,在你需要添加fbButton的类中添加下面的代码:
- (void)viewDidLoad
{[super viewDidLoad];// Create a FBLoginView to log the user in with basic, email and likes permissions// You should ALWAYS ask for basic permissions (basic_info) when logging the user in//[FBSettings setDefaultAppID:@"450051905053297"];FBLoginView *loginView = [[FBLoginView alloc] initWithReadPermissions:@[@"basic_info", @"email", @"user_likes"]];// Set this loginUIViewController to be the loginView button's delegateloginView.delegate = self;// Align the button in the center horizontallyloginView.frame = CGRectOffset(loginView.frame,(self.view.center.x - (loginView.frame.size.width / 2)),5);// Align the button in the center verticallyloginView.center = self.view.center;// Add the button to the view[self.view addSubview:loginView];
}
// This method will be called when the user information has been fetched
- (void)loginViewFetchedUserInfo:(FBLoginView *)loginViewuser:(id<FBGraphUser>)user {self.profilePictureView.profileID = user.id;self.nameLabel.text = user.name;
}// Implement the loginViewShowingLoggedInUser: delegate method to modify your app's UI for a logged-in user experience
- (void)loginViewShowingLoggedInUser:(FBLoginView *)loginView {self.statusLabel.text = @"You're logged in as";
}// Implement the loginViewShowingLoggedOutUser: delegate method to modify your app's UI for a logged-out user experience
- (void)loginViewShowingLoggedOutUser:(FBLoginView *)loginView {self.profilePictureView.profileID = nil;self.nameLabel.text = @"";self.statusLabel.text= @"You're not logged in!";
}// You need to override loginView:handleError in order to handle possible errors that can occur during login
- (void)loginView:(FBLoginView *)loginView handleError:(NSError *)error {NSString *alertMessage, *alertTitle;// If the user should perform an action outside of you app to recover,// the SDK will provide a message for the user, you just need to surface it.// This conveniently handles cases like Facebook password change or unverified Facebook accounts.if ([FBErrorUtility shouldNotifyUserForError:error]) {alertTitle = @"Facebook error";alertMessage = [FBErrorUtility userMessageForError:error];// This code will handle session closures since that happen outside of the app.// You can take a look at our error handling guide to know more about it// https://developers.facebook.com/docs/ios/errors} else if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryAuthenticationReopenSession) {alertTitle = @"Session Error";alertMessage = @"Your current session is no longer valid. Please log in again.";// If the user has cancelled a login, we will do nothing.// You can also choose to show the user a message if cancelling login will result in// the user not being able to complete a task they had initiated in your app// (like accessing FB-stored information or posting to Facebook)} else if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryUserCancelled) {NSLog(@"user cancelled login");// For simplicity, this sample handles other errors with a generic message// You can checkout our error handling guide for more detailed information// https://developers.facebook.com/docs/ios/errors} else {alertTitle = @"Something went wrong";alertMessage = @"Please try again later.";NSLog(@"Unexpected error:%@", error);}if (alertMessage) {[[[UIAlertView alloc] initWithTitle:alertTitlemessage:alertMessagedelegate:nilcancelButtonTitle:@"OK"otherButtonTitles:nil] show];}
}
好了,完工
Facebook登录(Login Button)相关推荐
- 第三方登陆实践之基于OAuth的FACEBOOK Web Login(最新版)
第三方登陆实践之基于OAuth的FACEBOOK Web Login(最新版) Facebook 登录简介 Facebook登陆实践 1.首先,登陆[Facebook开发者平台](https://de ...
- Google和facebook登录
准备工作是你要在官网创建应用,获取它们给你的id Google 登录 1,在google开发者后台创建应用 2,通过后台进行申请获取id 最近在试着用google登录,当然这只是简单的登录,直接用的它 ...
- Google登录和facebook登录相关
1:google登录 google登录和Facebook登录都是提供的自定义view按钮: <com.google.android.gms.common.SignInButtonandroid: ...
- Android 版 Facebook 登录
Android 版 Facebook SDK 让用户可以通过 Facebook 登录注册您的应用.通过 Facebook 登录您的应用时,用户可以向应用授予权限,以便您可以检索信息或以用户的身份在 F ...
- Android 应用程序集成FaceBook 登录及二次封装
1.首先在Facebook 开发者平台注册一个账号 https://developers.facebook.com/ 开发者后台 https://developers.facebook.com/ap ...
- facebook、twitter、facebook登录、whatsapp分享、微信分享
facebook.twitter.facebook 登录.whatsapp 分享.微信分享 几个概念 爬虫 所谓爬虫,是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本. html 元素图谱 对 ...
- (unity)新手接入Facebook登录,分享以及google登录,Android,IOS,OC接入篇
最近接Android,IOS的Facebook登录,分享 以及Google登录,分享流程以及遇到的问题整理. 一. Android接入 google登录 第一步,前往 [ firebase] http ...
- Android Facebook登录,进来看就对了
Facebook登录 一.目录 一.开始配置 Facebook Developers面板创建应用和基本配置 集成Facebook SDK 或者 使用依赖配置(二选一即可) 编辑资源和清单 开发秘钥散列 ...
- Android FaceBook登录问题记录
虽然按照官方文档 Facebook 登录 一步一步集成,但测试的时候还是遇到了问题,在这里记录一下.希望给其他出现相同问题的朋友一些借鉴. 问题1 提示没有权限,该账号不是测试账号 解决 在你申请的应 ...
最新文章
- 【SICP练习】84 练习2.56
- 顶配12699 元、没有5G,“浴霸三摄”的iPhone你会买吗?
- 获得目录下文件数和文件行数
- 【PAT乙级】1074 宇宙无敌加法器 (20 分)
- 基于DDD的.NET开发框架 - ABP工作单元(Unit of Work)
- 卡屏java_Java drawImage到屏幕上一卡一卡的
- 08_MySQL DQL_SQL99标准中的多表查询(内连接)
- Spring Boot笔记-@ComponentScan初步解析
- 玉置成実(Nami Tamaki) -《もしも愿いが…》单曲[MP3]
- mfc动态改变clip风格_echarts动态滑动平均滤波
- 快速搭建centos7
- 【Java项目实战】黄金矿工小游戏项目介绍
- [How to] ROOT, Backup Flash (MTKDroidTools, Spflashtool, CWM)
- Android 键盘映射
- 九个帮你提高开发效率的现代CSS框架
- 微信公众平台版面设计需要服务器,公众号版面设计,微信公众号中排版怎么弄...
- 重启c语言-找出总分最高的学生
- 《Dream it possible》【梦想能成真】——最具感人,激励,追梦的英文歌曲,华为主题曲。
- 正式揭牌!华为与清华北大、中科院共建博士后工作站!
- OD常用断点函数(转载)