8374
お世話になります  
NSURLSessionで通信処理を書いているのですが、complecationHandlerを書いた時は、正しく呼ばれるのですが、delegateで処理しようトルするとうまくいかずに困っております。

```

    let request : NSURLRequest = NSURLRequest(URL:xmlUrl)
    
    
                var config = NSURLSessionConfiguration.defaultSessionConfiguration()
    
                self.session = NSURLSession(configuration: config,
                    delegate: self,
                    delegateQueue: NSOperationQueue.mainQueue())
    
    
                func completionBlock(data: NSData!, response: NSURLResponse!, error: NSError!) -> Void {
                    if error != nil{
    
                        if self.initLoading{
                            SVProgressHUD.dismiss()
                        }else{
                            self.footerIndicator.stopAnimating()
                        }
                        self.isLoading = false
                        self.initLoading = false
                        self.nowLoading = false
    
                        //アラートを表示して終了
    
                        self.workview.reloadData()
    
                    }else{
                        if !notParseFlag{
                            var parser : NSXMLParser = NSXMLParser(data: data)
                            parser.delegate = self
                            parser.parse()
                        }
                    }
    
                }
    
    
                //var task:NSURLSessionDataTask = self.session.dataTaskWithRequest(request)
                var task : NSURLSessionDataTask = self.session.dataTaskWithRequest(request, completionHandler:completionBlock)
                task.resume()
    
```

だとうまくcomplecationBlcokに入ってきます

```

    let request : NSURLRequest = NSURLRequest(URL:xmlUrl)
    
    
                var config = NSURLSessionConfiguration.defaultSessionConfiguration()
    
                self.session = NSURLSession(configuration: config,
                    delegate: self,
                    delegateQueue: NSOperationQueue.mainQueue())
    
```

// func completionBlock(data: NSData!, response: NSURLResponse!, error:
NSError!) -> Void {  
// if error != nil{  
//  
// if self.initLoading{  
// SVProgressHUD.dismiss()  
// }else{  
// self.footerIndicator.stopAnimating()  
// }  
// self.isLoading = false  
// self.initLoading = false  
// self.nowLoading = false  
//  
// //アラートを表示して終了  
//  
// self.workview.reloadData()  
//  
// }else{  
// if !notParseFlag{  
// var parser : NSXMLParser = NSXMLParser(data: data)  
// parser.delegate = self  
// parser.parse()  
// }  
// }  
//  
// }

```

                var task:NSURLSessionDataTask = self.session.dataTaskWithRequest(request)
                //var task : NSURLSessionDataTask = self.session.dataTaskWithRequest(request, completionHandler:completionBlock)
                task.resume()
    
```

以下delegateメソッド

```

                    extension  WorkList {
                    func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveResponse response: NSURLResponse, completionHandler: (NSURLSessionResponseDisposition) -> Void){
                        println("request_start")
                    }
                    func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didBecomeDownloadTask downloadTask: NSURLSessionDownloadTask) {
                        println("............")
    
                    }
                    func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData data: NSData) {
                        println("request_finish")
    
                        if !notParseFlag{
                            var parser : NSXMLParser = NSXMLParser(data: data)
                            parser.delegate = self
                            parser.parse()
                        }
                    }
    
                    func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?){
                        if self.initLoading{
                            SVProgressHUD.dismiss()
                        }else{
                            self.footerIndicator.stopAnimating()
                        }
                        self.isLoading = false
                        self.initLoading = false
                        self.nowLoading = false
    
                        //アラートを表示して終了
    
                        self.workview.reloadData()
    
                    }
                    func URLSession(session: NSURLSession, didBecomeInvalidWithError error: NSError?){
                        println(error)
                    }
    
                    func cancelConnect(){
                        self.session.getTasksWithCompletionHandler
                        {
                                (dataTasks, uploadTasks, downloadTasks) -> Void in
    
                                self.cancelTasksByUrl(dataTasks     as [NSURLSessionTask])
    
                        }
                    }
    
                    private func cancelTasksByUrl(tasks: [NSURLSessionTask])
                    {
                        for task in tasks
                        {
                            task.cancel()
                        }
                    }
                }
    
```

request_startがデバッグにあるのは確認したので、どうしたもんかなと。。。  
よろしくお願いします。
CC BY-SA 3.0
2015-03-24T06:33:47.630
8308
2015-03-25T08:01:40.180
2015-03-24T11:59:10.033
3516
403
question
ios swift nsurlsession
NSURLSessionでdelegateメソッドが呼ばれない
解決しました。  
こちらを参照し、実装を以下のようにしました

```

          extension  WorkList {
          func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveResponse response: NSURLResponse, completionHandler: (NSURLSessionResponseDisposition) -> Void){
              println("request_start")
    
              if response.isKindOfClass(NSHTTPURLResponse){
                  let httpURLResponse:NSHTTPURLResponse = response as NSHTTPURLResponse
    
                  let statuscode = httpURLResponse.statusCode
                  println(statuscode)
    
                  if statuscode == 200{
                      println("success")
    
                      let disposition:NSURLSessionResponseDisposition = NSURLSessionResponseDisposition.Allow
                      completionHandler(disposition)
                  }else{
                      println("statusinvalid")
                      handleError()
                  }
              }
    
    
          }
          func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didBecomeDownloadTask downloadTask: NSURLSessionDownloadTask){
              println("didBecomeDownloadTask")
          }
    
          func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData data: NSData){
              if !notParseFlag{
                  var parser : NSXMLParser = NSXMLParser(data: data)
                  parser.delegate = self
                  parser.parse()
              }
          }
    
          func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?){
              println(error)
              if error != nil{
                  println("didCompleteWithError")
                  handleError()
              }
    
    
          }
          func URLSession(session: NSURLSession, didBecomeInvalidWithError error: NSError?){
              println(error)
    
              if error != nil{
                  println("didBecomeInvalidWithError")
                  handleError()
              }
          }
    
          func cancelConnect(){
              self.session.getTasksWithCompletionHandler
              {
                  (dataTasks, uploadTasks, downloadTasks) -> Void in
    
                  self.cancelTasksByUrl(dataTasks     as [NSURLSessionTask])
    
              }
          }
    
          func handleError(){
              if self.initLoading{
                  SVProgressHUD.dismiss()
              }else{
                  self.footerIndicator.stopAnimating()
              }
              self.isLoading = false
              self.initLoading = false
              self.nowLoading = false
    
              //アラートを表示して終了
              showAlert()
    
              self.workview.reloadData()
          }
    
          private func cancelTasksByUrl(tasks: [NSURLSessionTask])
          {
              for task in tasks
              {
                  task.cancel()
              }
          }
      }
    
```
CC BY-SA 3.0
2015-03-25T08:01:40.180
8374
2015-03-25T08:01:40.180
403
8308
answer