A.获取用户信息
1.需求
获取用户信息并储存
把用户昵称显示在“首页”界面导航栏的标题上
 
 
2.思路
使用微博API
将用户信息封装到HVWUser模型中
把获取的用户名存放到账户信息HVWAccountInfo模型中存储到沙盒
 
3.实现
 //  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);
}];
}
 
     因为用户数据模型HVWAccountInfo是在HVWAccountInfoTool中使用NSKeyedArchiver和NSKeyedUnarchiver进行文件读写的,所以要设置读写的属性名
 //  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"];
}
 
获取了用户昵称,并放在“首页”界面导航栏的标题按钮之后,还有根据文本长度计算按钮宽度,这里直接改写按钮的setTitle方法
 //  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] - 获取用户信息的更多相关文章

  1. [iOS微博项目 - 2.6] - 获取微博数据

    github: https://github.com/hellovoidworld/HVWWeibo   A.新浪获取微博API 1.读取微博API     2.“statuses/home_time ...

  2. [iOS微博项目 - 3.6] - 获取未读消息

    github: https://github.com/hellovoidworld/HVWWeibo   A.获取登陆用户未读消息 1.需求 获取所有未读消息,包括新微博.私信.@.转发.关注等 把未 ...

  3. Android之QQ授权登录获取用户信息

    有时候我们开发的app须要方便用户简单登录.能够让用户使用自己的qq.微信.微博登录到我们自己开发的app. 今天就在这里总结一下怎样在自己的app中集成QQ授权登录获取用户信息的功能. 首先我们打开 ...

  4. 微信第三方登陆,无需注册一键登录,获取用户信息,PHP实现方法

    今天讲讲利用微信oauth2实现第三方登陆的实现方法. 先说说前提吧! 首先你得是服务号,并且是经过认证的.这样微信会给你很多第三方接口的权限,如果是订阅号或者没有认证的服务号那就不用想了! 一开始你 ...

  5. 菜鸟-手把手教你把Acegi应用到实际项目中(7)-缓存用户信息

    首先讲讲EhCache.在默认情况下,即在用户未提供自身配置文件ehcache.xml或ehcache-failsafe.xml时,EhCache会依据其自身Jar存档包含的ehcache-fails ...

  6. Laravel OAuth2 (一) ---简单获取用户信息

    前言 本来要求是使用微信进行第三方登陆,所以想着先用 github 测试成功再用微信测试,可是最近拖了好久都还没申请好微信开放平台的 AppID ,所以就只写 github 的第三方登陆吧,估计微信的 ...

  7. mpvue小程序开发之 wx.getUserInfo获取用户信息授权

    一.背景 在使用美团的mpvue2.0框架搭建起小程序项目后,做获取用户信息时遇到一些问题:微信小程序更新api后,获取用户信息只能通过button上的绑定方法 来获取用户信息,vue上方法绑定不能直 ...

  8. PHP 微信公众号开发 - 获取用户信息

    项目微信公众号开发,记录获取用户微信号信息,和用户openid 1,登录微信公众平台 点击登录微信公众平台 2,获取公众号开发信息 登陆之后在 开发->基本配置 3,设置IP白名单 在这里添加服 ...

  9. [iOS微博项目 - 4.3] - 设置每条微博边框样式

    github: https://github.com/hellovoidworld/HVWWeibo A.设置每条微博边框样式 1.需求 不需要分割线 每个微博之间留有一定的间隙   2.思路 直接设 ...

随机推荐

  1. HDU 1850 (Nim博弈 取胜方案数) Being a Good Boy in Spring Festival

    考虑到Bouton定理的证明过程,设n个数的Nim和(异或和)为X,其最高位的1在第k位,那么n个数中一定有个y的第k为也是个1. 将y的数量变为X xor y,那么n的数的Nim和为0,便转为先手必 ...

  2. [转] POJ数学问题

    转自:http://blog.sina.com.cn/s/blog_6635898a0100magq.html 1.burnside定理,polya计数法 这个大家可以看brudildi的<组合 ...

  3. js,正则应用

    //获取URL中的request参数 function getUrlParam(name) { var reg = new RegExp("(^|&)" + name + ...

  4. mysql-主从复制(一)

    1)用户授权 grant all privileges on share.* to 'abc'@'192.168.1.105' identified by '123456' 2)开启mysql的bin ...

  5. (转)beanUtil接口和类(有空的时候去看,到时候删除这个说明)

    Jakarta Commons项目提供了相当丰富的API,我们之前了解到的Commons Lang只是众多API的比较核心的一小部分而已.Commons下面还有相当数量的子项目,用于解决各种各样不同方 ...

  6. mac下编译optool方法

    参考地址:http://www.mopsled.com/2016/build-optool-osx/ 1.git clone https://github.com/alexzielenski/opto ...

  7. 理解matplotlib绘图

    matplotlib是基于Python语言的开源项目,旨在为Python提供一个数据绘图包.Matplotlib 可能是 Python 2D-绘图领域使用最广泛的套件.它能让使用者很轻松地将数据图形化 ...

  8. Java文件下载的几种方式

    public HttpServletResponse download(String path, HttpServletResponse response) { try { // path是指欲下载的 ...

  9. Selenium2Library系列 keywords 之 _ElementKeywords

    #公有方法: (1)current_frame_contains(self, text, loglevel='INFO') (2)current_frame_should_not_contain(se ...

  10. 我的window平台下的软件

    SocksCap64-Portable-3.0(配合google drive 使用) ShadowsocksR-win-3.7.4 dropbox xx-net chrome switchyomega ...