网络访问和本地存储学习
开发环境
- Mac OS
- Objective-C
- Xcode
实验目的
- 学习使用NSURLSession或AFNetworking库进行网络访问
- 学习iOS沙盒机制,进行文件读写操作
项目实现
一、创建一个Xcode项目
点击File->New->Project,选择ios下的Single View App,创建一个项目。
二、项目结构
AppDelegate
主要是将登陆页面设置为根页面。
登录页
登录页面需要用户输入用户名和密码,点击按钮进行登录,因此需要两个UITextField和一个UIbutton。由于需要网络访问来判断登录是否成功,所以需要发起网络请求,与期中项目一样,使用AFNetworking库来进行网络访问。
具体的第三方库安装与导入可以查看这篇博客 向登录的api发起post请求,当输入正确的用户名和密码后会收到success回复,反之为fail,当登录成功后跳转到新的页面——个人页面。
个人页面
个人信息的获取同样需要网络访问,向相应的api发起get请求后,将返回的信息存到字典中,再通过不同的key来获得不同value,从而区分出用户的姓名、等级、邮箱和电话。再将其放入相应的UIlabel中显示。
相应的代码如下:
图片浏览页面
由于有多张图片,因此将图片放入tableview中,一个图片占一个section。页面有三个按钮,分别是加载、清空图片和清除缓存。按照要求,若缓存为空则从网络上获取图片并放入缓存中。否则,将缓存中的图片显示出来。
加载
从网络上获取图片:
- (UIImage *)getImageFromURL:(NSString *)fileURL
{
UIImage *result;
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
result = [UIImage imageWithData:data];
return result;
}
UIImage *img1 = [self getImageFromURL:(@"https://hbimg.huabanimg.com/d8784bbeac692c01b36c0d4ff0e072027bb3209b106138-hwjOwX_fw658")];
点击加载按钮时,会先读取缓存中的内容,若缓存中不为空,则将缓存中的内容路径取出,放入一个数组中。
NSString * path = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"];
NSFileManager * fm = [NSFileManager defaultManager];
NSArray * arr = [fm contentsOfDirectoryAtPath:path error:nil];
arr = [arr sortedArrayUsingSelector:@selector(compare:)];
for(id key in arr){
NSString * temp = [[NSString alloc] init];
temp = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"];
temp = [temp stringByAppendingPathComponent:key];
[self.imgarr addObject:temp]; //将图片在缓存中的路径放入imgarr中
}
由于将图片放入tabelview中,因此加载完后,需要执行tableview的reloaddata方法。
tableview需要实现的代理方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"cell";
NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%ld%ld", (long)[indexPath section], (long)[indexPath row]];//以indexPath来唯一确定cell
customcell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//cell.selectionStyle = UITableViewCellSelectionStyleNone; //禁止点击
if (cell == nil) {
cell = [[customcell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
UIImage *im = [UIImage imageWithContentsOfFile:[self.imgarr objectAtIndex:indexPath.section]];//将imgarr中存放的图片路径读出,成为图片。
[cell setImageView:im];
}
return cell;
}
customcell的实现:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"cell";
NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%ld%ld", (long)[indexPath section], (long)[indexPath row]];//以indexPath来唯一确定cell
customcell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//cell.selectionStyle = UITableViewCellSelectionStyleNone; //禁止点击
if (cell == nil) {
cell = [[customcell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
UIImage *im = [UIImage imageWithContentsOfFile:[self.imgarr objectAtIndex:indexPath.section]];
[cell setImageView:im];
}
return cell;
}
清空图片
点击清空图片按钮时,只需将imgarr中的所有元素清除并再次reloaddata即可。
[self.imgarr removeAllObjects];
[self.tableView reloadData];
清除缓存
先用
NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
获取缓存的路径。
再通过NSEnumerator枚举遍历缓存中的数据,将其依个remove。
完整代码如下:
NSFileManager *manager = [NSFileManager defaultManager];
NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
NSEnumerator *filesEnumerator = [[manager subpathsAtPath:path] objectEnumerator];
NSString *filePath;
while ((filePath = [filesEnumerator nextObject]) != nil) {
NSString *string = [path stringByAppendingPathComponent:filePath];
[manager removeItemAtPath:string error:nil];
}
[self.imgarr removeAllObjects];
实现效果