NetWorkingTool
2016-04-05 07:27
267 查看
.h
@protocol NetWorkingToolDelegate <NSObject> - (void)bringValue: (id) result; @end @interface NetWorkingTool : NSObject + (void) toolWithURl: (NSString *) strURL block: (void (^) (id result)) block; @end
.m
@implementation NetWorkingTool + (void) toolWithURl: (NSString *) strURL block: (void (^) (id result)) block { NSURL *url = [NSURL URLWithString: strURL]; NSMutableURLRequest *requet = [NSMutableURLRequest requestWithURL: url]; NSURLSession *session = [NSURLSession sharedSession]; NSURLSessionTask *task = [session dataTaskWithRequest: requet completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { // 把数据从子线程移到主线程 dispatch_queue_t queue = dispatch_get_main_queue(); dispatch_async(queue, ^{ id result = [NSJSONSerialization JSONObjectWithData: data options:NSJSONReadingMutableContainers error:nil]; block (result); }); }]; [task resume]; } @end
eg.
- (void) createData { [NetWorkingTool toolWithURl:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php" block:^(id result) { NSDictionary *dic = result; self.movieArr = dic[@"result"]; // [self.tableView reloadData]; }]; }
相关文章推荐
- 数据结构(一)-----线性表
- 【Unity】4.1 创建组件
- 动态规划之Edit Step Ladders
- spring的框架集,简化的编程模型
- 缓存学习笔记-1
- 存储的发展的过程
- 【Unity】4.0 第4章 创建基本的游戏场景
- 电工助手 V1.3
- 【Unity】3.6 导入图片资源
- Kafka安装部署
- Pinyin Comparison 拼音辨别 V1.1.1
- 【Unity】3.5 导入音频文件
- 【Unity】3.4 将现有的3ds Max模型导入到Unity
- 【Unity】3.3 用3ds Max 2015制作模型并将其导入到Unity
- python 的环境搭建
- 软考中高项学员:2016年4月4日作业
- Build 2016,你可能忽视的几个细节
- HoloLens开发手记-配置开发环境 Install the tools
- 最有价值的Linux博文链接(51cto)
- LeetCode 229. Majority Element II(众数II)