首页 技术 正文
技术 2022年11月15日
0 收藏 843 点赞 2,901 浏览 4016 个字

github: https://github.com/hellovoidworld/HVWWeibo
 A.获取用户信息1.需求获取用户信息并储存把用户昵称显示在“首页”界面导航栏的标题上 [iOS微博项目 – 3.4] – 获取用户信息 2.思路使用微博API将用户信息封装到HVWUser模型中把获取的用户名存放到账户信息HVWAccountInfo模型中存储到沙盒[iOS微博项目 – 3.4] – 获取用户信息[iOS微博项目 – 3.4] – 获取用户信息[iOS微博项目 – 3.4] – 获取用户信息[iOS微博项目 – 3.4] – 获取用户信息 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;
}

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,154
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,623
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,465
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,239
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,874
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,042