Facebook 登录自定义按钮
2016-11-02 14:16
666 查看
<pre name="code" class="objc"> [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_updateContent:) name:FBSDKProfileDidChangeNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_accessTokenChanged:) name:FBSDKAccessTokenDidChangeNotification object:nil];
- (void)_updateContent:(NSNotification *)notification {
<span style="white-space:pre"> </span> NSInteger slot = 0; FBSDKProfile *profile = notification.userInfo[FBSDKProfileChangeNewKey];
<span style="white-space:pre"> </span> if (profile) { SUCacheItem *cacheItem = [SUCache itemForSlot:slot];
<span style="white-space:pre"> </span>cacheItem.profile = profile;
<span style="white-space:pre"> </span> [SUCache saveItem:cacheItem slot:slot];
}}
// Observe a new token, so save it to our SUCache and update
- (void)_accessTokenChanged:(NSNotification *)notification{
<span style="white-space:pre"> </span>FBSDKAccessToken *token = notification.userInfo[FBSDKAccessTokenChangeNewKey];
<span style="white-space:pre"> </span>if (!token) { [FBSDKAccessToken setCurrentAccessToken:nil];
<span style="white-space:pre"> </span>[FBSDKProfile setCurrentProfile:nil];
<span style="white-space:pre"> </span>}else {
<span style="white-space:pre"> </span>NSInteger slot = 0;
<span style="white-space:pre"> </span> SUCacheItem *item = [SUCache itemForSlot:slot] ?: [[SUCacheItem alloc] init];
<span style="white-space:pre"> </span>if (![item.token isEqualToAccessToken:token]) {
<span style="white-space:pre"> </span> <span style="white-space:pre"> </span> item.token = token; [SUCache saveItem:item slot:slot];
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
}
- (IBAction)facebookLoginBtnClick:(id)sender {
<span style="white-space:pre"> </span>NSInteger slot = 0; FBSDKAccessToken *token = [SUCache itemForSlot:slot].token;
<span style="white-space:pre"> </span> if (token) {
<span style="white-space:pre"> </span>[self fbAutoLoginWithToken:token];
<span style="white-space:pre"> </span>}else {
<span style="white-space:pre"> </span>[self fbNewLogin];
<span style="white-space:pre"> </span>}
}
- (void)fbAutoLoginWithToken:(FBSDKAccessToken *)token{
// We have a saved token, issue a request to make sure it's still valid.
<span style="white-space:pre"> </span>[FBSDKAccessToken setCurrentAccessToken:token];
<span style="white-space:pre"> </span>FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:nil];
<span style="white-space:pre"> </span>[request setGraphErrorRecoveryDisabled:YES];
<span style="white-space:pre"> </span>[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
// Since we're only requesting /me, we make a simplifying assumption that any error // means the token is bad.
<span style="white-space:pre"> </span>if (error) {
<span style="white-space:pre"> </span>[[[UIAlertView alloc] initWithTitle:nil message:@"The user token is no longer valid."
<span style="white-space:pre"> </span>delegate:nil
<span style="white-space:pre"> </span>cancelButtonTitle:@"OK"
<span style="white-space:pre"> </span>otherButtonTitles:nil] show];
<span style="white-space:pre"> </span>[SUCache deleteItemInSlot:0];;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}];
}
- (void)fbNewLogin{
<span style="white-space:pre"> </span>FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
<span style="white-space:pre"> </span>[login logInWithReadPermissions: @[@"public_profile",@"email"]
<span style="white-space:pre"> </span>fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
<span style="white-space:pre"> </span>if (error) { DLog(@"Process error");
<span style="white-space:pre"> </span> }else if (result.isCancelled) {
<span style="white-space:pre"> </span>DLog(@"Cancelled");
<span style="white-space:pre"> </span>}else {
<span style="white-space:pre"> </span> DLog(@"Logged in");
<span style="white-space:pre"> </span> FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:result.token.userID
<span style="white-space:pre"> </span>parameters:@{@"fields": @"id,name,email"}
HTTPMethod:@"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result,NSError *error)
{ // Handle the result DLog(@"%@,%@,%@",result[@"id"],result[@"name"],result[@"email"]);
}];
}
}];
}
相关文章推荐
- Facebook(一)自定义登录按钮,登陆完毕后清除缓存的Session信息
- 自定义网页QQ登录按钮
- iOS不得姐项目--登录模块的布局,设置文本框占位文字颜色,自定义内部控件竖直排列的按钮
- 利用FaceBook实现第三方登陆(自定义登陆按钮,非官方按钮)并获取用户数据
- iOS_APP自定义退出登录按钮
- Cocos2d-x自定义按钮类控制精灵攻击----之游戏开发《赵云要格斗》(2) cocos2dx 3.3移植版
- tableview中自定义自定义左滑显示编辑按钮
- Project Web Access 2007自定义FORM验证登录实现
- IOS 自定义导航栏题目和返回按钮标题
- 如何在TabBar上自定义添加个性按钮
- 如何在SAP屏幕上自定义按钮?
- 为FCK添加自定义的功能按钮
- 利用Forms验证(自定义、角色提供程序、单点登录)登陆 (转)
- jqgrid中edittype为text、CheckBox、select、textarea、function等,为jqgrid添加自定义按钮
- Android 实现按钮自定义风格
- ALV在标准按钮上增加自定义按钮,并实现文本及功能互相切换
- MFC 如何将自定义按钮,添加到对话框的成员变量中?
- Android 自定义Button按钮显示样式(正常、按下、获取焦点)
- Android自定义控件系列二:自定义开关按钮(一)
- CAS自定义登录页面