【问题标题】:Passing response to controller while converting NSURLConnection to NSURLSession在将 NSURLConnection 转换为 NSURLSession 时将响应传递给控制器
【发布时间】:2016-10-27 01:43:59
【问题描述】:

我有类似实现 NSURLConnectionDelegate 方法的服务 API 类。我注意到其中一些方法已被弃用,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 时将响应传递给控制器​​。

【问题讨论】:

    标签: ios nsurlconnection nsurlsession


    【解决方案1】:

    类似这样的:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2021-04-29
        • 2012-09-25
        • 2019-12-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-14
        • 1970-01-01
        相关资源
        最近更新 更多