1.协议方法,负责传数据.因为传值需要两种情况,所以是id类型
<span style="font-size:18px;">- (void)bringResult:(id)result;</span>
2.然后根据方法调用传过来的网址,获取数据并且解析数据.因为是类方法,没有对象,所以代理人直接在函数里设置
<span style="font-size:18px;">// 根据方法调用时候传过来的网址,获取数据,并且解析数据,负责解析数据
+ (void)networkingWithStrURL:(NSString *)strURL delegate:(id<NetworkingToolDelegate>)delegate;
// 加号方法,没有对象,所以指定代理人时要这么写</span>
3.正常的解析数据(block会执行的比较慢,没有办法写return,因为block有延时性,所以需要用协议或者block传值)
<span style="font-size:18px;">+ (void)networkingWithStrURL:(NSString *)strURL delegate:(id<NetworkingToolDelegate>)delegate{
//
NSURL *url = [NSURL URLWithString:strURL];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
// block是会执行的比较慢,没有办法写return,因为block有延时性,所以需要用协议或者block传值
// JCD
// 找到当前工程的主线程
dispatch_queue_t mainQueue = dispatch_get_main_queue();
// 异步将子线程内容显示到主线程
dispatch_async(mainQueue, ^{
// 数据解析
id result = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
// 代理人去执行协议方法
[delegate bringResult:result];
});
}];
[task resume];</span>
}
4.创建数据的方法中调用函数
<span style="font-size:18px;">// [NetworkingTool networkingWithStrURL:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php" delegate:self];</span>
5.实现协议方法
<span style="font-size:18px;">// 协议方法
- (void)bringResult:(id)result{
NSLog(@"%@", result);
}</span>
1.block传值,传值的时候传一个block,先将block改名成(Block),block(result)通过block把值返回
// block传值
// 把block改新的名字 叫Block
typedef void(^Block)(id result);
// block传值
+ (void)networkingWithStrURL:(NSString *)strURL block:(Block)block;
// block传值, 模仿系统block
+ (void)networkingWithStrURL:(NSString *)strURL block:(Block)block{
NSURL *url = [NSURL URLWithString:strURL];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionTask *task = [session dataTaskWithRequest:request 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:0 error:nil];
// 通过block把值返回
block (result);
});
}];
[task resume];
}
3.(1)post,GET方法两种,需要判断.先建一个枚举,表示类型
// POST,GET
typedef NS_ENUM(NSUInteger, MethodType) {
GETType,
POSTType,
};
(2)方法中需要添加类型,bodyStr,还有block
// 在方法添加
+ (void)networkingWithStrURL:(NSString *)strURL type:(MethodType)type bodyStr:(NSString *)bodyStr block:(Block)block;
(3)实现
+ (void)networkingWithStrURL:(NSString *)strURL type:(MethodType)type bodyStr:(NSString *)bodyStr block:(Block)block{
NSURL *url = [NSURL URLWithString:strURL];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 先判断类型,如果是POST需要三句话
if (type == POSTType) {
[request setHTTPMethod:@"POST"];
NSData *data = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data];
}
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
id result = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
block(result);
}];
[task resume];
}
(4)主函数实现
// // GET和POST两种网络请求
// [NetworkingTool networkingWithStrURL:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php" type:GETType bodyStr:nil block:^(id result) {
// NSLog(@"%@", result);
// }];