IOS之网络访问和本地存储
- 开发环境
- 实验目的
- 实验内容
- 实验结果
- 实验结果展示
- 登陆页面
- 个人中心
- 我的图片
- 实验代码分析
- 登陆页面
- 个人中心
- 我的图片
- 我的心得
开发环境
- Mac OS
- Objective-C
- Xcode
实验目的
- 学习使用NSURLSession或AFNetworking库进行网络访问
- 学习iOS沙盒机制,进行文件读写操作
实验内容
实现一个简单的图片浏览应用,有两个页面
- 用户页面,首先要通过用户名和密码进行登录,然后展示用户的基本信息。
- 图片浏览页面,包含一个Label,一个图片列表(可以用UICollectionView或UITableView),以及三个按钮(“加载” “清空” “删除缓存”)。
- 点击"加载"按钮,若Cache中没有缓存的文件,则加载网络图片并显示在图片列表中,要求:图片下载完成前,显示loading图标;图片下载后,存入沙盒的Cache中。
- 点击"加载"按钮,若Cache中已存在图片文件,则直接从Cache中读取出图片并显示。
- 点击"清空"按钮,清空图片列表中的所有图片。
- 点击"删除缓存"按钮,删除存储在Cache中的图片文件。
- 用户个人信息的获取通过访问网络API实现。
实验结果
实验结果展示
登陆页面
输入账号:MOSAD 密码:1234 点击登录按钮进入个人中心页面
个人中心
该页面从后端获取了个人信息,点击我的照片进入图片详情页面
我的图片
该页面三个按钮:
- 加载图片按钮如果本地没有缓存,那么从url链接拿去图片,并保存在本地;如果本地有缓存,则加载缓存
- 清空图片按钮会清空当前的图片列表,但不会清除本地缓存
- 删除按钮会清空列表并清除本地缓存
实验代码分析
登陆页面
该页面使用三个UILabel,两个UITextField,一个UIButton
@property (nonatomic , retain) UILabel *l1;
@property (nonatomic , retain) UILabel *l2;
@property (nonatomic , retain) UILabel *l3;
@property (nonatomic , retain) UITextField *t1;
@property (nonatomic , retain) UITextField *t2;
@property (nonatomic , retain) UIButton *btn;
点击按钮运用后端接口验证,判断登录是否成功
-(void)buttonClicked:(id)sender{
NSString *url = @"http://172.18.176.202:3333/hw3/signup";
NSDictionary *parameters = @{@"name": _t1.text,
@"pwd": _t2.text
};
AFHTTPSessionManager *manage = [AFHTTPSessionManager manager];
// 设置请求体为JSON
manage.requestSerializer = [AFJSONRequestSerializer serializer];
// 设置响应体为JSON
manage.responseSerializer = [AFJSONResponseSerializer serializer];
[manage POST:url parameters:parameters headers:nil progress:^(NSProgress * _Nonnull uploadProgress){} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
AppDelegate * app =(AppDelegate*)[UIApplication sharedApplication].delegate;
MyViewController *page = [[MyViewController alloc]init];
page.title = @"个人中心";
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:page];
app.window.rootViewController = nav;
[app. window makeKeyAndVisible ];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];
}
个人中心
该页面运用一个UITableView,将section设置为圆角矩形,在tableview的Header上设置一个自定义的UIView来显示头像以及名称,页面信息由后端接口传入
顶部自定义Header
self.backgroundColor = [UIColor clearColor];
_image = [[UIImageView alloc] init];
_name = [[UILabel alloc] init];
[self addSubview:_image];
[self addSubview:_name];
[_image mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.mas_top).with.offset(15);
make.left.equalTo(self.mas_left).with.offset(10);
make.width.equalTo(self.mas_width).multipliedBy(0.2);
make.height.equalTo(self.mas_width).multipliedBy(0.2);
}];
_image.layer.masksToBounds = YES;
// _image.layer.cornerRadius = _image.layer.frame.size.width / 2;
_image.contentMode = UIViewContentModeScaleToFill;
_image.backgroundColor = [UIColor whiteColor];
_image.layer.borderColor = [[UIColor blackColor] CGColor];
_image.layer.borderWidth = 0.2f;
[_name mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.mas_top).with.offset(15);
make.left.equalTo(self.image.mas_right).with.offset(10);
make.width.equalTo(self.mas_width).multipliedBy(0.6);
make.height.equalTo(self.mas_width).multipliedBy(0.2);
}];
self.name.numberOfLines = 2;
_name.userInteractionEnabled=YES;
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0, 320,5)];
view.backgroundColor = [UIColor clearColor];
if (section == 0) {
HeaderView *h = [[HeaderView alloc] init];
h.image.image = [UIImage imageNamed:@"timg"];
h.name.text = self.Data[@"name"];
h.name.font = [UIFont systemFontOfSize:30];
return h;
}
return view;
}
tableview设置圆角
后端信息获取
NSString *url = @"http://172.18.176.202:3333/hw3/getinfo?name=MOSAD";
AFHTTPSessionManager *manage = [AFHTTPSessionManager manager];
// 设置请求体为JSON
manage.requestSerializer = [AFJSONRequestSerializer serializer];
// 设置响应体为JSON
manage.responseSerializer = [AFJSONResponseSerializer serializer];
[manage GET:url parameters:nil headers:nil progress:^(NSProgress * _Nonnull downloadProgress) {} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSDictionary *returndata = (NSDictionary*)responseObject;
self.Data = returndata;
[self.tableView reloadData];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];
我的图片
该页面用了三个UIButton,一个UITableview,以及存储数据的NSDictionary和NSMutableArray,以及运用了IOS的本地缓存
页面布局
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0, 320,5)];
view.backgroundColor = [UIColor clearColor];
if (section == 0) {
ImageHeader *h = [[ImageHeader alloc] init];
h.title.text = @"我的图片";
[h.load setTitle:@"加载图片" forState:UIControlStateNormal];
[h.clear setTitle:@"清空图片" forState:UIControlStateNormal];
[h.load setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[h.clear setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[h.load addTarget:self action:@selector(loadpic:) forControlEvents:UIControlEventTouchUpInside];
[h.clear addTarget:self action:@selector(clearpic:) forControlEvents:UIControlEventTouchUpInside];
return h;
}
return view;
}
加载图片按钮
清空图片按钮
-(void)clearpic:(id)sender{
[_imagearray removeAllObjects];
[_tableView reloadData];
}
删除缓存按钮
- (void)onClickedOKbtn {
[_imagearray removeAllObjects];
[self delcache];
[_tableView reloadData];
}
-(void)delcache{
NSFileManager *manager = [NSFileManager defaultManager];
NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
NSEnumerator *filesEnumerator = [[manager subpathsAtPath:path] objectEnumerator];//枚举 caches下所有文件路径
NSString *filePath;//文件名
while ((filePath = [filesEnumerator nextObject]) != nil) {
NSString *string = [path stringByAppendingPathComponent:filePath];
[manager removeItemAtPath:string error:nil];
}
}
我的心得
这次作业不仅熟悉了之前学到的网络编程的知识,并且对于IOS本地缓存的操作有了了解和学习,这次作业遇到的问题主要是图片的重复保存,以及相关的缓存删除,在学习一些资料后,问题得到解决