FacebFacebook 处理自动登录问题(iOS)
2016-09-30 12:03
761 查看
在本地保存密码的工具
我们可以直接使用facebook的官方demo里面的两个类来管理我们的密码一定要添加Security.framework这个依赖库
SUCacheItem这个类实现了<NSSecureCoding>协议,对
profile和token进行归档和反归档。
@interface SUCacheItem : NSObject<NSSecureCoding> @property (nonatomic, strong) FBSDKProfile *profile; @property (nonatomic, strong) FBSDKAccessToken *token; @end
SUCache这个类用来对SUCacheItem进行增删改查操作。
#import "SUCacheItem.h" // A helper class to store items in keychain. @interface SUCache : NSObject + (SUCacheItem *)itemForSlot:(NSInteger)slot; + (void)saveItem:(SUCacheItem *)item slot:(NSInteger)slot; + (void)deleteItemInSlot:(NSInteger)slot; + (void)clearCache; @end
登录
现在的登录逻辑需要改一下,我们先判断一下本地储存token没有,如果本地游储存,判断是否是有效的,如果有效,就直接登录进去。如果无效了,就删除本地的token,进行登录操作。
代码修改成下面这样。
#pragma mark - login
//自定义login button的点击事件
- (IBAction)loginBtnClicked:(id)sender {
NSInteger slot = 0;
FBSDKAccessToken *token = [SUCache itemForSlot:slot].token;
if (token) {
[self autoLoginWithToken:token];
}
else {
[self newLogin];
}
}
- (void)autoLoginWithToken:(FBSDKAccessToken *)token {
[FBSDKAccessToken setCurrentAccessToken:token];
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:nil];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
//token过期,删除存储的token和profile
if (error) {
NSLog(@"The user token is no longer valid.");
NSInteger slot = 0;
[SUCache deleteItemInSlot:slot];
[FBSDKAccessToken setCurrentAccessToken:nil];
[FBSDKProfile setCurrentProfile:nil];
}
//做登录完成的操作
else {
}
}];
}
- (void)newLogin {
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login
logInWithReadPermissions: @[@"public_profile"]
fromViewController:self
handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
NSLog(@"facebook login result.grantedPermissions = %@,error = %@",result.grantedPermissions,error);
if (error) {
NSLog(@"Process error");
} else if (result.isCancelled) {
NSLog(@"Cancelled");
} else {
NSLog(@"Logged in");
}
}];
}
token 和 profile的保存
在通知事件里如果获取到了新的token或者profile,就把它们保存到SUCacheItem的实例中去。注册通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_updateContent:) name:FBSDKProfileDidChangeNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_accessTokenChanged:) name:FBSDKAccessTokenDidChangeNotification object:nil];
在通知的事件中处理token和profile的保存
#pragma mark - Notification - (void)_updateContent:(NSNotification *)notification { FBSDKProfile *profile = notification.userInfo[FBSDKProfileChangeNewKey]; [self labelDisplayWithProfile:profile]; } - (void)_accessTokenChanged:(NSNotification *)notification { FBSDKAccessToken *token = notification.userInfo[FBSDKAccessTokenChangeNewKey]; if (!token) { [FBSDKAccessToken setCurrentAccessToken:nil]; [FBSDKProfile setCurrentProfile:nil]; } else { NSInteger slot = 0; SUCacheItem *item = [SUCache itemForSlot:slot] ?: [[SUCacheItem alloc] init]; if (![item.token isEqualToAccessToken:token]) { item.token = token; [SUCache saveItem:item slot:slot]; } } }
相关文章推荐
- IOS-UIview的AutoresizingMask自动伸缩解析(主要处理UIView加载出来看不见的问题)
- 根据Facebook内存的管理使用,浅谈在iOS上自动检测内存泄漏问题
- 使用mc-tool实现empathy的自动登录与处理密钥环问题
- 根据Facebook内存的管理使用,浅谈在iOS上自动检测内存泄漏问题
- IOS处理点空白处不自动失去焦点的问题
- FaceBook第三方登录问题处理
- 系统自动登录及盘符无法双击打开问题处理 推荐
- C# 关于winFrom控制网页的自动登录的问题(网页刷屏器的制作原理)
- sqlserver2005jdbc解决自动自动增长列统一处理问题纪实
- userinit.exe丢失导致不能登录系统问题处理 推荐
- 解决通过Cookie进行网站自动登录的安全性问题
- 怎样解决启动WindowsXP进入登录欢迎界面时,自动开启小键盘NumLock的问题
- 怎样解决启动WindowsXP进入登录欢迎界面时,自动开启小键盘NumLock的问题
- sql server2005 jdbc解决自动自动增长列统一处理问题纪实
- 解决笔记本登录时自动启动小键盘问题
- 解决笔记本登录时自动启动小键盘问题
- sqlserver2005jdbc解决自动增长列统一处理问题纪实
- Win XP 登录后自动注销的问题,winxp登录后自动注销的问题,xp登录后自动注销的问题
- 在验证模式为Forms的情况下,如何解决自动登录问题. (已解决)
- crystal report 图像字段处理和提示登录框问题的解决办法