nsurlsession
How to get cookie from a NSURLSession with Swift?
The Swift rendition might look something like: let task = session.dataTask(with: request) { data, response, error in guard let url = response?.url, let httpResponse = response as? HTTPURLResponse, let fields = httpResponse.allHeaderFields as? [String: String] else { return } let cookies = HTTPCookie.cookies(withResponseHeaderFields: fields, for: url) HTTPCookieStorage.shared.setCookies(cookies, for: url, mainDocumentURL: nil) for cookie in cookies … Read more
Resume NSUrlSession on iOS10
This problem arose from currentRequest and originalRequest NSKeyArchived encoded with an unusual root of “NSKeyedArchiveRootObjectKey” instead of NSKeyedArchiveRootObjectKey constant which is “root” literally and some other misbehaves in encoding process of NSURL(Mutable)Request. I detected that in beta 1 and filed a bug (no. 27144153 in case you want duplicate). Even I sent an email to … Read more
Using NSURLSession from a Swift command line program
You can use a semaphore to block the current thread and wait for your URL session to finish. Create the semaphore, kick off your URL session, then wait on the semaphore. From your URL session completion callback, signal the semaphore. You could use a global flag (declare a volatile boolean variable) and poll that from … Read more
What is difference between NSURLSessionDataTask vs NSURLSessionDownloadTask
NSURLSessionDataTask : Data tasks exchange data using NSData. NSURLSessionDataTask is not supported in Background Sessions. Data tasks send and receive data using NSData objects. Data tasks are intended for short, often interactive requests from your app to a server. Data tasks can return data to your app one piece at a time after each piece … Read more
How to make NSURLSession POST request in Swift
Swift 4 post example with json payload- func postAction(_ sender: Any) { let Url = String(format: “your url”) guard let serviceUrl = URL(string: Url) else { return } let parameterDictionary = [“username” : “Test”, “password” : “123456”] var request = URLRequest(url: serviceUrl) request.httpMethod = “POST” request.setValue(“Application/json”, forHTTPHeaderField: “Content-Type”) guard let httpBody = try? JSONSerialization.data(withJSONObject: parameterDictionary, … Read more
How To Download Multiple Files Sequentially using NSURLSession downloadTask in Swift
Your code won’t work because URLSessionDownloadTask runs asynchronously. Thus the BlockOperation completes before the download is done and therefore while the operations fire off sequentially, the download tasks will continue asynchronously and in parallel. While there are work-arounds one can contemplate (e.g., recursive patterns initiating one request after the prior one finishes, non-zero semaphore pattern … Read more
Unexpected Non-Void Return Value In Void Function (Swift 2.0)
You have a problem because your line: return minions does not return from your function. Instead, it returns from the completion handler in dataTaskWithRequest. And it shouldn’t be doing so because that closure is a void function. The problem which you have results from the fact that dataTaskWithRequest is an asynchronous operation. Which means that … Read more
How to get server response data in NSURLSession without completion block
A couple of thoughts: First, instantiate your session with a delegate, because background sessions must have a delegate: NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:kSessionIdentifier]; self.session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil]; Second, instantiate your NSURLSessionUploadTask without a completion handler, because tasks added to a background session cannot use completion blocks. Also note, I’m using a file URL … Read more