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

时间:2016-10-27 01:43:59

标签: ios nsurlconnection nsurlsession

我有类似服务API类的东西,它实现了NSURLConnectionDelegate方法。我注意到其中一些方法已被弃用,Apple现在建议使用NSURLSession。我创建了这个服务API自己的委托,调用类将实现该委托,并在收到响应时执行。我正在寻找在使用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

在ServiceAPI实施文件中:

- (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];
}

上课提出请求并获得回复:

@interface CallingController : UITableViewController<ServiceAPIDelegate>
@end

实施文件CallingController:

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

在使用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类,我的控制器只是将请求传递给服务API来进行调用,一旦响应回来,它就会传递给控制器​​。如何在使用NSURLSession时将响应传递给控制器​​。

2 个答案:

答案 0 :(得分:0)

您可以通过初始化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;

答案 1 :(得分:0)

这样的事情:

   @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
}