[iOS微博项目 - 3.4] - 获取用户信息





// HVWHomeViewController.m
/** 获取用户信息 */
- (void) setupUserInfo {
// 设置参数
NSMutableDictionary *param = [NSMutableDictionary dictionary];
// 访问令牌
HVWAccountInfo *accountInfo = [HVWAccountInfoTool accountInfo];
param[@"access_token"] = accountInfo.access_token;
// 用户uid
param[@"uid"] = accountInfo.uid; // 发送请求
[HVWNetworkTool get:@"https://api.weibo.com/2/users/show.json" parameters:param success:^(id responseObject) {
// 获取用户信息
HVWUser *user = [HVWUser objectWithKeyValues:responseObject];
HVWAccountInfo *accountInfo = [HVWAccountInfoTool accountInfo];
accountInfo.screen_name = user.screen_name;
[HVWAccountInfoTool saveAccountInfo:accountInfo]; // 设置导航栏标题
[self.titleButton setTitle:accountInfo.screen_name forState:UIControlStateNormal];
} failure:^(NSError *error) {
HVWLog(@"获取用户信息失败!error:%@", error);
}];
}
// HVWAccountInfoTool.m
/** 从文件获取accountInfo */
+ (HVWAccountInfo *) accountInfo {
HVWAccountInfo *accountInfo = [NSKeyedUnarchiver unarchiveObjectWithData:[NSData dataWithContentsOfFile:accountInfoPath]]; // 需要判断是否过期
NSDate *now = [NSDate date];
if ([now compare:accountInfo.expires_time] != NSOrderedAscending) { // now->expires_data 非升序, 已经过期
accountInfo = nil;
} return accountInfo;
} /** 存储accountInfo到文件 */
+ (void) saveAccountInfo:(HVWAccountInfo *) accountInfo {
[NSKeyedArchiver archiveRootObject:accountInfo toFile:accountInfoPath];
}
// HVWAccountInfo.m
#pragma mark - NSCoding
/** 从文件解析对象调用 */
- (id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super init]) {
self.access_token = [aDecoder decodeObjectForKey:@"access_token"];
self.expires_in = [aDecoder decodeObjectForKey:@"expires_in"];
self.expires_time = [aDecoder decodeObjectForKey:@"expires_time"];
self.uid = [aDecoder decodeObjectForKey:@"uid"];
self.screen_name = [aDecoder decodeObjectForKey:@"screen_name"];
} return self;
} /** 把对象写入文件调用 */
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:self.access_token forKey:@"access_token"];
[aCoder encodeObject:self.expires_in forKey:@"expires_in"];
[aCoder encodeObject:self.expires_time forKey:@"expires_time"];
[aCoder encodeObject:self.uid forKey:@"uid"];
[aCoder encodeObject:self.screen_name forKey:@"screen_name"];
}
// HVWNavigationBarTitleButton.m
/** 重写setTitle,根据文本改变宽度 */
- (void)setTitle:(NSString *)title forState:(UIControlState)state {
[super setTitle:title forState:state]; NSDictionary *param = @{NSFontAttributeName: self.titleLabel.font};
CGFloat titleWidth = [title boundingRectWithSize:CGSizeMake(MAXFLOAT, self.height) options:NSStringDrawingUsesLineFragmentOrigin attributes:param context:nil].size.width; self.width = titleWidth + self.height + ;
}
// HVWHomeViewController.m
/** 设置导航栏 */
- (void) setupNavigationBar {
// 添加导航控制器按钮
// 左边按钮
self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"navigationbar_friendsearch" hightlightedImage:@"navigationbar_friendsearch_highlighted" target:self selector:@selector(searchFriend)]; // 右边按钮
self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImage:@"navigationbar_pop" hightlightedImage:@"navigationbar_pop_highlighted" target:self selector:@selector(pop)]; // 设置标题按钮
HVWNavigationBarTitleButton *titleButton = [[HVWNavigationBarTitleButton alloc] init];
titleButton.height = ; // 保存到成员属性
self.titleButton = titleButton; // 设置导航栏标题
HVWAccountInfo *accountInfo = [HVWAccountInfoTool accountInfo];
NSString *navTitle = @"首页";
if (accountInfo.screen_name) {
navTitle = accountInfo.screen_name;
}
[titleButton setTitle:navTitle forState:UIControlStateNormal]; [titleButton setImage:[UIImage imageWithNamed:@"navigationbar_arrow_down"] forState:UIControlStateNormal];
// 设置背景图片
[titleButton setBackgroundImage:[UIImage resizedImage:@"navigationbar_filter_background_highlighted"] forState:UIControlStateHighlighted]; // 监听按钮点击事件,替换图标
[titleButton addTarget:self action:@selector(titleButtonClickd:) forControlEvents:UIControlEventTouchUpInside]; self.navigationItem.titleView = titleButton;
}
[iOS微博项目 - 3.4] - 获取用户信息的更多相关文章
- [iOS微博项目 - 2.6] - 获取微博数据
github: https://github.com/hellovoidworld/HVWWeibo A.新浪获取微博API 1.读取微博API 2.“statuses/home_time ...
- [iOS微博项目 - 3.6] - 获取未读消息
github: https://github.com/hellovoidworld/HVWWeibo A.获取登陆用户未读消息 1.需求 获取所有未读消息,包括新微博.私信.@.转发.关注等 把未 ...
- Android之QQ授权登录获取用户信息
有时候我们开发的app须要方便用户简单登录.能够让用户使用自己的qq.微信.微博登录到我们自己开发的app. 今天就在这里总结一下怎样在自己的app中集成QQ授权登录获取用户信息的功能. 首先我们打开 ...
- 微信第三方登陆,无需注册一键登录,获取用户信息,PHP实现方法
今天讲讲利用微信oauth2实现第三方登陆的实现方法. 先说说前提吧! 首先你得是服务号,并且是经过认证的.这样微信会给你很多第三方接口的权限,如果是订阅号或者没有认证的服务号那就不用想了! 一开始你 ...
- 菜鸟-手把手教你把Acegi应用到实际项目中(7)-缓存用户信息
首先讲讲EhCache.在默认情况下,即在用户未提供自身配置文件ehcache.xml或ehcache-failsafe.xml时,EhCache会依据其自身Jar存档包含的ehcache-fails ...
- Laravel OAuth2 (一) ---简单获取用户信息
前言 本来要求是使用微信进行第三方登陆,所以想着先用 github 测试成功再用微信测试,可是最近拖了好久都还没申请好微信开放平台的 AppID ,所以就只写 github 的第三方登陆吧,估计微信的 ...
- mpvue小程序开发之 wx.getUserInfo获取用户信息授权
一.背景 在使用美团的mpvue2.0框架搭建起小程序项目后,做获取用户信息时遇到一些问题:微信小程序更新api后,获取用户信息只能通过button上的绑定方法 来获取用户信息,vue上方法绑定不能直 ...
- PHP 微信公众号开发 - 获取用户信息
项目微信公众号开发,记录获取用户微信号信息,和用户openid 1,登录微信公众平台 点击登录微信公众平台 2,获取公众号开发信息 登陆之后在 开发->基本配置 3,设置IP白名单 在这里添加服 ...
- [iOS微博项目 - 4.3] - 设置每条微博边框样式
github: https://github.com/hellovoidworld/HVWWeibo A.设置每条微博边框样式 1.需求 不需要分割线 每个微博之间留有一定的间隙 2.思路 直接设 ...
随机推荐
- 精选37条强大的常用linux shell命令组合
任务 命令组合 1 删除0字节文件 find . -type f -size 0 -exec rm -rf {} \;find . type f ...
- UVa 1587 Box
题意:给出6个矩形的长和宽,问是否能够构成一个长方体 先假设一个例子 2 3 3 4 2 3 3 4 4 2 4 2 排序后 2 3 2 3 3 4 3 4 4 2 4 2 如果要构成一个长方体的话, ...
- ui/ue设计师应该了解的原型设计软件
前段实践整理过一些原型设计用的软件,这里分享一下,喜欢对更多的PM战线的童鞋有所裨益.(因为交互原型工具Axure ui设计师都很常用了,文中就不专门介绍了) 首先分下类: •1.交互原型(产品能做什 ...
- Cookie存储中文报错:java.lang.IllegalArgumentException: Control character in cookie value or attribute.(转)
项目中做自动登录和保存密码时,Cookie报错Java.lang.IllegalArgumentException,上google查了下 在http://hi.baidu.com/xtxycy/blo ...
- Android 系统 reboot
/*********************************************************************** * Android 系统 reboot * 说明: * ...
- id to load is required for loading----id被要求加载exception
表示id主键 没有找到,可能是数据库中的主键设置了not null 而你没有给予主键 还有就是没有传递主键到 数据库中
- [转]深入理解Flash Player重绘
这个是tencent flash team的一篇文章,但团队的博客已经关闭了,所以就在这里备份下吧~ 后来有人把这篇文章又发布到9ria上了,引发了一些讨论,其中有两位大神发言了,内容在原文下方. 9 ...
- Python 字典(Dictionary) get()方法
描述 Python 字典(Dictionary) get() 函数返回指定键的值,如果值不在字典中返回默认值. 语法 get()方法语法: dict.get(key, default=None) 参数 ...
- mate代码详解
1.用以说明生成工具(如MICROSOFT FRONTPAGE 4.0)等: 2.向搜索引擎说明你的网页的关键词: 3.告诉搜索引擎你的站点的主要内容: 4.告诉搜索引擎你的站点的制作的作者: 5. ...
- Solution multisite htaccess cleanURL
My solution to getting Clean URL working with my multisite setup drupal 4.7 I added Alias to my http ...