您的位置:首页 > 其它

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];
}];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: