网络 — 你的程序离开了它就不能生存下去!苹果的Foundation framework中的NSURLConnection又非常难以理解, 不过这里有一个可以使用的替代品:AFNetworking。AFNetworking 包括了所有你需要与在线资源交互的内容,从web services到文件下载。当你的程序在下载一个大文件期间,AFNetworking还能确保你的UI是可以响应的。今天将从三个简单地任务来实现AFNetWorking的极小部分功能(九牛一毛,冰山一角)。在写案例之前,首先AFNetWorking.h文件添加进入。
任务一:通过AFNetWorking框架下载网络数据
1 //通过AFNetWorking框架下载网络数据 2 //1.先创建AFHTTPSessionManager对象 3 AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithBaseURL:[NSURL URLWithString:@"http://127.0.0.1/userManager/"]]; 4 /** 5 * 参数详解: 6 1.要从网络中的到的文件 7 2.传递参数 8 3.成功执行的操作 9 4.失败执行的操作 10 */ 11 //注意:在cities.json之前不能出现路径“/”,否则会出现异常错误。 12 [manager GET:@"cities.json" parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) { 13 NSArray *provinces =[responseObject objectForKey:@"provinces"]; 14 for(NSString *province in provinces) 15 { 16 NSLog(@"%@",province); 17 } 18 } failure:^(NSURLSessionDataTask *task, NSError *error) { 19 NSLog(@"网络获取失败:%@",error); 20 }];
1 #import "ViewController.h" 2 3 @interface ViewController ()<UITableViewDataSource> 4 @property(strong,nonatomic)UITableView *tableView; 5 @property(strong,nonatomic)UIRefreshControl *freshControl; 6 @property(strong,nonatomic)NSMutableArray *datas; 7 @property(strong,nonatomic)NSURL *url; 8 @end 9 10 @implementation ViewController 11 12 - (void)viewDidLoad { 13 [super viewDidLoad]; 14 //1.先创建TableView 15 self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds]; 16 self.tableView.dataSource = self; 17 [self.view addSubview:self.tableView]; 18 //创建刷新控件 19 self.freshControl = [[UIRefreshControl alloc]initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 100)]; 20 [self.freshControl addTarget:self action:@selector(reload:) forControlEvents:UIControlEventValueChanged]; 21 [self.tableView addSubview:self.freshControl]; 22 // [self.tableView.tableHeaderView addSubview:self.freshControl]; 23 /** 24 * 如果是使用的ViewoController则添加到table内,如果采用的tableViewController,可以添加到self.tableView.tableHeaderView中。 25 */ 26 self.datas = [NSMutableArray array]; 27 //设置网络连接路径 28 self.url = [NSURL URLWithString:[NSString stringWithFormat:@"http://127.0.0.1/userManager/0.png"]]; 29 [self reload:nil]; 30 } 31 -(void)reload:(UIRefreshControl *)sender 32 { 33 //添加数据 34 for(int i = 0;i<3;i++) 35 { 36 NSString *string = [NSString stringWithFormat:@"name%d",arc4random_uniform(9)]; 37 [self.datas addObject:string]; 38 } 39 //刷新表格 40 [self.tableView reloadData]; 41 //结束刷新 42 [self.freshControl endRefreshing]; 43 } 44 45 #pragma mark - TableView数据源 46 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 47 { 48 return self.datas.count; 49 } 50 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 51 { 52 //1.根据reuseindentifier先到对象池中去找重用的单元格 53 static NSString *reuseIndetifier = @"myCell"; 54 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIndetifier]; 55 //2.如果没有找到需要自己创建单元格对象 56 if (cell == nil) { 57 cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIndetifier]; 58 } 59 //3.设置单元格对象的内容 60 cell.textLabel.text = [self.datas objectAtIndex:indexPath.row]; 61 //从网络中获取图片, 62 [self setImageWithURL:self.url placeholderImage:[UIImage imageNamed:@"profile-image-placeholder"] setCell:cell]; 63 return cell; 64 } 65 -(void)setImageWithURL:(NSURL *)url 66 placeholderImage:(UIImage *)placeholderImage setCell:(UITableViewCell *)cell 67 { 68 //设置图片占位符 69 [cell.imageView setImage:placeholderImage]; 70 //对路径进行发送请求 71 NSURLRequest *request = [NSURLRequest requestWithURL:self.url]; 72 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) 73 { 74 /** 75 * 如果从网络中下载图片成功,则显示下载的图片,否则使用占位符。 76 */ 77 if (connectionError) 78 { 79 [cell.imageView setImage:placeholderImage]; 80 } 81 else 82 { 83 UIImage *image = [UIImage imageWithData:data]; 84 [cell.imageView setImage:image]; 85 } 86 }]; 87 } 88 @end
程序运行效果图:
原文:http://www.cnblogs.com/xjf125/p/4838932.html