一、网络通信
1. 获取服务器数据
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json",nil];
//拼接请求参数
NSDictionary *parameter = @{@"user" : @"user",
@"title" : @"title",
@"description" : @"description",
@"panorama" : @"panorama",
@"filesize" : @"filesize",
@"width" : @"width",
@"thumbnail" : @"thumbnail",
@"likes" : @"likes",
@"collections" : @"collections",
@"created" : @"created"
};
// 取出最前面的微博(最新的微博,ID最大的微博)
HWStatusFrame *firstStatusF = [self.statusFrames firstObject];
[mgr GET:@"http://114.214.164.105:8000/api/panoramas/" parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
//获取相应用户的微博微博字典数组,由于服务器上的是最新发出的排在最后和我们这个微博要求相反,所以要倒过来拿
NSArray *reverseArray = [[responseObject reverseObjectEnumerator] allObjects];
//李明杰mj的框架
/*
MJExtension是一套字典和模型之间互相转换的超轻量级框架
MJExtension能完成的功能
字典(JSON) --> 模型(Model)
模型(Model) --> 字典(JSON)
字典数组(JSON Array) --> 模型数组(Model Array)
模型数组(Model Array) --> 字典数组(JSON Array)
*/
//这里将字典数组转成模型数组
/*
如何从plist中读取字典,并转换成对象模型
http://www.cnblogs.com/fxiaoquan/archive/2015/04/18/4438126.html
下面的直接利用第三份开源框架字典转模型工具
*/
NSArray *newStatuses = [HWStatus mj_objectArrayWithKeyValuesArray:reverseArray];
//将HWStatus数组转换成HWstatusFrame数组
NSMutableArray *Frames = [NSMutableArray array];
//这个newFrames包含所有数据,所以要加上判断
for(HWStatus *status in newStatuses)
{
if(status.created > firstStatusF.status.created)
{
HWStatusFrame *f = [[HWStatusFrame alloc] init];
f.status = status;
[Frames addObject:f];
}
else
break;
}
[self.statusFrames addObjectsFromArray:Frames];
//可以获取数据
//NSLog(@"%@",reverseArray);
//刷新表格
[self.tableView reloadData];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error == %@",error);
}];
//点击全景图片进行展示,此处用原图
#pragma mark - 代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//获取的全部内容的key
HWStatusCell *cell = [HWStatusCell cellWithTableView:tableView];
//statusFrames所有数据模型的集合
cell.statusFrame = self.statusFrames[indexPath.row];
//拼url
NSString *panoString = [NSString stringWithFormat:@"http://114.214.164.105:8000%@",cell.statusFrame.status.panorama];
PanoViewController *panoView = [[PanoViewController alloc] init];
panoView.urlString = panoString;
NSString *biaoti = [NSString stringWithFormat:@"%@",cell.statusFrame.status.title];
panoView.title = biaoti;
// 当test1控制器被push的时候,test1所在的tabbarcontroller的tabbar会自动隐藏
// 当test1控制器被pop的时候,test1所在的tabbarcontroller的tabbar会自动显示
// test1.hidesBottomBarWhenPushed = YES;
// self.navigationController === HWNavigationController
//push进来的会被拦截
[self.navigationController pushViewController:panoView animated:YES];
//[self presentViewController:panoView animated:YES completion:nil];
}
3. 上传数据
AFHTTPRequestOperationManager *mgr= [AFHTTPRequestOperationManager manager];
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"user"] = @"1";
params[@"description"] = @"Panoramic Picture";
params[@"title"] = self.textView.text;
[mgr POST:@"http://114.214.164.105:8000/api/panoramas/" parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
UIImage *image = [self.photosView.photos firstObject];
NSData *data = UIImageJPEGRepresentation(image, 1.0);
[formData appendPartWithFileData:data name:@"panorama" fileName:@"PIC" mimeType:@"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
[MBProgressHUD showSuccess:@"发送成功"];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[MBProgressHUD showSuccess:@"发送失败"];
NSLog(@"%@", self.textView.text);
NSLog(@"%@", error.localizedDescription);
NSLog(@"%@", error.localizedFailureReason);
NSLog(@"%@", error.userInfo);
}];
二、手写代码而非拖控件需要做的事
下列函数都在AppDelegate中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{