URLSession.datatask with request block not called in background

If you want downloads to progress after your app is no longer in foreground, you have to use background session. The basic constraints of background sessions are outlined in Downloading Files in Background, and are essentially: Use delegate-based URLSession with background URLSessionConfiguration. Use upload and download tasks only, with no completion handlers. In iOS, Implement … Read more

How to send POST and GET request?

Sending POST and GET requests in iOS is quite easy; and there’s no need for an additional framework. POST Request: We begin by creating our POST‘s body (ergo. what we’d like to send) as an NSString, and converting it to NSData. objective-c NSString *post = [NSString stringWithFormat:@”test=Message&this=isNotReal”]; NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; Next up, … Read more

NSURLSession: How to increase time out for URL requests?

ObjC NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration]; sessionConfig.timeoutIntervalForRequest = 30.0; sessionConfig.timeoutIntervalForResource = 60.0; Swift let sessionConfig = URLSessionConfiguration.default sessionConfig.timeoutIntervalForRequest = 30.0 sessionConfig.timeoutIntervalForResource = 60.0 let session = URLSession(configuration: sessionConfig) What docs say timeoutIntervalForRequest and timeoutIntervalForResource specify the timeout interval for the request as well as the resource. timeoutIntervalForRequest – The timeout interval to use when waiting … Read more

Undocumented NSURLErrorDomain error codes (-1001, -1003 and -1004) using StoreKit

All error codes are on “CFNetwork Errors Codes References” on the documentation (link) A small extraction for CFURL and CFURLConnection Errors: kCFURLErrorUnknown = -998, kCFURLErrorCancelled = -999, kCFURLErrorBadURL = -1000, kCFURLErrorTimedOut = -1001, kCFURLErrorUnsupportedURL = -1002, kCFURLErrorCannotFindHost = -1003, kCFURLErrorCannotConnectToHost = -1004, kCFURLErrorNetworkConnectionLost = -1005, kCFURLErrorDNSLookupFailed = -1006, kCFURLErrorHTTPTooManyRedirects = -1007, kCFURLErrorResourceUnavailable = -1008, kCFURLErrorNotConnectedToInternet … Read more

NSURLSession with NSBlockOperation and queues

The harshest criticisms of synchronous network requests are reserved for those who do it from the main queue (as we know that one should never block the main queue). But you’re doing it on your own background queue, which addresses the most egregious problem with synchronous requests. But you’re losing some wonderful features that asynchronous … Read more

How to make HTTP request in Swift?

You can use URL, URLRequest and URLSession or NSURLConnection as you’d normally do in Objective-C. Note that for iOS 7.0 and later, URLSession is preferred. Using URLSession Initialize a URL object and a URLSessionDataTask from URLSession. Then run the task with resume(). let url = URL(string: “http://www.stackoverflow.com”)! let task = URLSession.shared.dataTask(with: url) {(data, response, error) … 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

NSURLSession concurrent requests with Alamofire

Yes, this is expected behavior. One solution is to wrap your requests in custom, asynchronous NSOperation subclass, and then use the maxConcurrentOperationCount of the operation queue to control the number of concurrent requests rather than the HTTPMaximumConnectionsPerHost parameter. The original AFNetworking did a wonderful job wrapping the requests in operations, which made this trivial. But … Read more

Send POST request using NSURLSession

You could try using a NSDictionary for the params. The following will send the parameters correctly to a JSON server. NSError *error; NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil]; NSURL *url = [NSURL URLWithString:@”[JSON SERVER”]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; [request addValue:@”application/json” forHTTPHeaderField:@”Content-Type”]; [request addValue:@”application/json” forHTTPHeaderField:@”Accept”]; [request setHTTPMethod:@”POST”]; … Read more

tech