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;
}