.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"];
}];
}