在将 NSURLConnection 转换为 NSURLSession 时将响应传递给控制器

Passing response to controller while converting NSURLConnection to NSURLSession

我有类似服务 API class 的东西,它实现了 NSURLConnectionDelegate 方法。我注意到其中一些方法已被弃用,Apple 现在建议使用 NSURLSession。我创建了此服务 API 自己的委托,调用 class 将实现该委托,并在收到响应时执行该委托。我正在寻找如何在使用 NSURLSession 时做类似的事情。

我现在的 NSURLConnection 东西:

@class ServiceAPI;
@protocol ServiceAPIDelegate <NSObject>

- (void)getResponseData:(NSData *)responseData;

@end

@interface ServiceAPI : NSObject<NSURLCoDelegate>
- (void)httpServiceRequest:(NSMutableURLRequest *)serviceRequest;
@property (nonatomic, weak) id <ServiceAPIDelegate> delegate;
@end

服务中API实现文件:

- (void)httpServiceRequest:(NSMutableURLRequest *)serviceRequest {
    //[[NSURLSession sharedSession] dataTaskWithRequest:serviceRequest] resume];
    self.requestConnection = [NSURLConnection connectionWithRequest:serviceRequest delegate:self];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    [self.delegate getResponseData:self.responseData sender:self];
}

Class 发出请求并得到响应:

@interface CallingController : UITableViewController<ServiceAPIDelegate>
@end

实现文件CallingController:

- (void)getResponseData:(NSData *)responseData {
   // Do something with response. 
}

如何让调用 class 在使用 NSURLSession 时像 NSURLConnection 一样处理响应方法。虽然阅读 NSURLSession 看起来请求和响应是使用完成处理程序一起处理的,如下所示:

NSURLSessionDataTask *dataTask = [session dataTaskWithURL:[NSURL URLWithString:@"https://itunes.apple.com/search?term=apple&media=software"] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    NSLog(@"%@", json);
}]; 

我仍然想要一个服务 API class 我的控制器只会将请求传递给服务 API 进行调用,一旦响应返回,它就会传递给控制器​​。如何在使用 NSURLSession 时将响应传递给控制器​​。

您仍然可以通过初始化 NSURLSessionDataTask 来实现您想要的,而无需完成块。这样,它将改为调用委托方法。

例如:

NSURLSessionDataTask *dataTask = [session dataTaskWithURL:[NSURL URLWithString:@"https://itunes.apple.com/search?term=apple&media=software"]];
[dataTask resume];

实施NSURLSessionDataDelegate

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
                                 didReceiveResponse:(NSURLResponse *)response
                                  completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler;

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
                                     didReceiveData:(NSData *)data;

同时实施 NSURLSessionTaskDelegate 以了解请求何时完成。

/* Sent as the last message related to a specific task.  Error may be
 * nil, which implies that no error occurred and this task is complete. 
 */
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
                           didCompleteWithError:(nullable NSError *)error;

像这样:

   @class ServiceAPI;

    @protocol ServiceAPIDelegate <NSObject>

    - (void)getResponseData:(NSData *)responseData;

    @end

    @interface ServiceAPI : NSObject<NSURLSessionDelegate>
    - (void)httpServiceRequest:(NSMutableURLRequest *)serviceRequest;
    @property (nonatomic, weak) id <ServiceAPIDelegate> delegate;
    @end

实施中

- (void)httpServiceRequest:(NSMutableURLRequest *)serviceRequest {
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *downloadTask =

    [session dataTaskWithRequest:serviceRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

        [self.delegate getResponseData:data sender:self];

    }];

    [downloadTask resume];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    // no use
}