使用 AFNetworking-Synchronous
链接: https://github.com/paulmelnikow/AFNetworking-Synchronous
同步方法举例
- (void)sendSynRequest {
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://www.sojson.com/open/api/weather/"]];
manager.completionQueue = dispatch_queue_create("AFNetworking+Synchronous", NULL);
NSError *error = nil;
NSData *result = [manager syncGET:@"json.shtml?city=%E6%9D%AD%E5%B7%9E"
parameters:nil
task:NULL
error:&error];
NSLog(@"同步: %@", result);
}
异步方法举例
- (void)sendAsynRequest {
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://www.sojson.com/open/api/weather/"]];
manager.completionQueue = dispatch_queue_create("AFNetworking+Synchronous", NULL);
[manager GET:@"json.shtml?city=%E6%9D%AD%E5%B7%9E"
parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
//
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"异步: %@", responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//
}];
NSLog(@"我先执行啦~~~~");
}
附上 demo 链接: https://github.com/JessieHu/AFNetworking_Syn