NSURLSession completion block not called

前端 未结 6 755
天命终不由人
天命终不由人 2021-02-18 19:07
var session = NSURLSession.sharedSession()
session.dataTaskWithRequest(urlRequest, 
                            completionHandler: {(data: NSData!, 
                             


        
相关标签:
6条回答
  • 2021-02-18 19:36

    I face the same problem and I solved it by

    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * data, NSURLResponse * response, NSError * error) {
    
        if (!error)
        {
            NSLog(@"Data is %@",data);
            NSLog(@"Response is %@",response);
            NSLog(@"Error is %@",error);
        }
     }];
    
    [dataTask resume];
    

    And check that you are added the App Transport Security Settings in your info.plist.

    0 讨论(0)
  • 2021-02-18 19:36

    This is a fairly unique case, but if you're using a URLSession inside a unit test, you'll need to setup the expectations. Otherwise, your test case will end and it will appear that your request is never returning. Swift 3.0.1.

        let expect = expectation(description: "dataTaskWithRequest - completes")
    
        if let url = URL(string: "https://www.google.com/") {
    
            let request = URLRequest(url: url)
    
            URLSession.shared.dataTask(with: request) { ( data, response, error) in
    
                print(data.debugDescription)
                print(response.debugDescription)
                print(error.debugDescription)
    
                expect.fulfill()
    
            }.resume()
    
            waitForExpectations(timeout: 10, handler: nil)
        }
    
    0 讨论(0)
  • 2021-02-18 19:38

    So I tried calling it like this

    session.dataTaskWithRequest(urlRequest, 
                            completionHandler: {(data: NSData!, 
                                                 response: NSURLResponse!,                       
                                                 error: NSError!) in
                                                      print(data)
                                                      print(response)
                                                      print(error)
                                               }).resume()
    

    And it worked.

    Seems like I have to call resume() on a default suspended session task.

    0 讨论(0)
  • 2021-02-18 19:41

    Are you using playgrounds??

    If you are, you should be careful to include:

     XCPSetExecutionShouldContinueIndefinitely(continueIndefinitely: true)
    

    In order to make the playground wait for the callback

    0 讨论(0)
  • 2021-02-18 19:41

    It'll be something like this in Swift 2.x

    NSURLSession.sharedSession().dataTaskWithRequest(theRequest) { (data, response , error) in
        print(response)
    }.resume()
    
    0 讨论(0)
  • 2021-02-18 19:44

    You can also use it simply by :-

    let url = "api url"
    
    let nsURL = NSURL
    
    let task = NSURLSession.sharedSession().dataTaskWithURL(nsURL) {
    (data, response, error) in
       // your condition on success and failure
    }
    
    task.resume()
    
    0 讨论(0)
提交回复
热议问题