<small id='kQdBz'></small><noframes id='kQdBz'>

      <i id='kQdBz'><tr id='kQdBz'><dt id='kQdBz'><q id='kQdBz'><span id='kQdBz'><b id='kQdBz'><form id='kQdBz'><ins id='kQdBz'></ins><ul id='kQdBz'></ul><sub id='kQdBz'></sub></form><legend id='kQdBz'></legend><bdo id='kQdBz'><pre id='kQdBz'><center id='kQdBz'></center></pre></bdo></b><th id='kQdBz'></th></span></q></dt></tr></i><div id='kQdBz'><tfoot id='kQdBz'></tfoot><dl id='kQdBz'><fieldset id='kQdBz'></fieldset></dl></div>

      • <bdo id='kQdBz'></bdo><ul id='kQdBz'></ul>

      <legend id='kQdBz'><style id='kQdBz'><dir id='kQdBz'><q id='kQdBz'></q></dir></style></legend>
      <tfoot id='kQdBz'></tfoot>
      1. NSURLSession 委托与 completionHandler

        时间:2023-09-12

        <tfoot id='1iA1C'></tfoot>
        • <bdo id='1iA1C'></bdo><ul id='1iA1C'></ul>

            <legend id='1iA1C'><style id='1iA1C'><dir id='1iA1C'><q id='1iA1C'></q></dir></style></legend>
              <i id='1iA1C'><tr id='1iA1C'><dt id='1iA1C'><q id='1iA1C'><span id='1iA1C'><b id='1iA1C'><form id='1iA1C'><ins id='1iA1C'></ins><ul id='1iA1C'></ul><sub id='1iA1C'></sub></form><legend id='1iA1C'></legend><bdo id='1iA1C'><pre id='1iA1C'><center id='1iA1C'></center></pre></bdo></b><th id='1iA1C'></th></span></q></dt></tr></i><div id='1iA1C'><tfoot id='1iA1C'></tfoot><dl id='1iA1C'><fieldset id='1iA1C'></fieldset></dl></div>

                    <tbody id='1iA1C'></tbody>
                1. <small id='1iA1C'></small><noframes id='1iA1C'>

                2. 本文介绍了NSURLSession 委托与 completionHandler的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我一直使用完成处理程序.使用 NSURLConnection,现在使用 NSURLSession.这导致我的代码非常不整洁,尤其是我在请求中的请求中请求.

                  I've always used completion handlers. With NSURLConnection and now with NSURLSession. It's led to my code being really untidy, especially I have request within request within request.

                  我想尝试在 NSURLSession 中使用委托来实现我用 NSURLConnection 做的一些杂乱无章的事情.

                  I wanted to try using delegates in NSURLSession to implement something I've done untidily with NSURLConnection.

                  于是我创建了一个NSURLSession,并创建了一个dataTask:

                  So I created a NSURLSession, and created a dataTask:

                  NSURLSessionDataTask *dataTask = [overallSession dataTaskWithURL:url
                                                                    completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                                        if(error == nil)
                                                                        {
                                                                            NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
                                                                            NSLog(@"Data = %@",text);
                                                                        }
                  
                                                                    }];
                  
                      [dataTask resume];
                  

                  现在我有一个 completionHandler 用于响应,我将如何切换到委托来管理响应和数据?我可以从这个委托中添加另一个 dataTask 吗?使用此 dataTask 创建并放入会话中的 cookie?

                  Right now I have a completionHandler for the response, how would I switch to delegates to manage the response and data? And can I add another dataTask from the delegate of this one? Using the cookies that this dataTask created and placed into the session?

                  推荐答案

                  如果要添加自定义委托类,需要实现NSURLSessionDataDelegateNSURLSessionTaskDelegate协议至少.

                  If you want to add a custom delegate class, you need to implement the NSURLSessionDataDelegate and NSURLSessionTaskDelegate protocols at the minimum.

                  使用方法:

                   - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response
                   completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler {
                  
                      receivedData=nil; receivedData=[[NSMutableData alloc] init];
                      [receivedData setLength:0];
                  
                      completionHandler(NSURLSessionResponseAllow);
                  }
                  

                  NSURLSessionDataDelegate

                  -(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
                     didReceiveData:(NSData *)data {
                  
                      [receivedData appendData:data];
                  }
                  

                  NSURLSessionTaskDelegate

                   - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
                  didCompleteWithError:(NSError *)error {
                   if (error) {
                    // Handle error
                   }
                  else {
                     NSDictionary* response=(NSDictionary*)[NSJSONSerialization JSONObjectWithData:receivedData options:kNilOptions error:&tempError];
                      // perform operations for the  NSDictionary response
                  }
                  

                  如果您想将委托代码(中间层)与您的调用类分开(通常最好的做法是为网络调用提供单独的类/层),NSURLSession 的委托必须是:-

                  If you want to separate the delegate code (middle layer) from your calling class (generally its good practice to have separate class/layer for network calls), the delegate of NSURLSession has to be :-

                  NSURLSession *session=[NSURLSession sessionWithConfiguration:sessionConfig delegate:myCustomDelegateClass delegateQueue:nil];
                  

                  参考链接:

                  1. NSURLSession 类参考
                  2. iOS NSURLSession 示例(HTTP GET、POST、后台下载)
                  3. 从 NSURLConnection 到 NSURLSession

                  这篇关于NSURLSession 委托与 completionHandler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:[[UIApplication sharedApplication] delegate] 的简写? 下一篇:快速关闭模态视图控制器时传递数据

                  相关文章

                    <bdo id='yzkVW'></bdo><ul id='yzkVW'></ul>
                  <tfoot id='yzkVW'></tfoot>
                3. <legend id='yzkVW'><style id='yzkVW'><dir id='yzkVW'><q id='yzkVW'></q></dir></style></legend>

                    <small id='yzkVW'></small><noframes id='yzkVW'>

                      <i id='yzkVW'><tr id='yzkVW'><dt id='yzkVW'><q id='yzkVW'><span id='yzkVW'><b id='yzkVW'><form id='yzkVW'><ins id='yzkVW'></ins><ul id='yzkVW'></ul><sub id='yzkVW'></sub></form><legend id='yzkVW'></legend><bdo id='yzkVW'><pre id='yzkVW'><center id='yzkVW'></center></pre></bdo></b><th id='yzkVW'></th></span></q></dt></tr></i><div id='yzkVW'><tfoot id='yzkVW'></tfoot><dl id='yzkVW'><fieldset id='yzkVW'></fieldset></dl></div>