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 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 do I use NSOperationQueue with NSURLSession?

Your intuition here is correct. If issuing many requests, having an NSOperationQueue with maxConcurrentOperationCount of 4 or 5 can be very useful. In the absence of that, if you issue many requests (say, 50 large images), you can suffer timeout problems when working on a slow network connection (e.g. some cellular connections). Operation queues have … 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

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/NSURLConnection HTTP load failed on iOS 9

Found solution: In iOS9, ATS enforces best practices during network calls, including the use of HTTPS. From Apple documentation: ATS prevents accidental disclosure, provides secure default behavior, and is easy to adopt. You should adopt ATS as soon as possible, regardless of whether you’re creating a new app or updating an existing one. If you’re … 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