I have successfully coded a network request to fetch JSON data from our server. The remote function is written in ColdFusion. However, the code is quite lengthy and involved. I also noticed in the API there are other seemingly very similar ways one can go about making network requests, such as NSURLSession
. (I know NSURLConnection
will be deprecated in the future, replaced by NSURLSession
.) I would like to know what is the best way to make a network request - using modern code that is not going to be deprecated in the near future. Is NSURLSession
the best way to go?
Here are my requirements:
- Fetch JSON data from a function in a .cfc file on the server
- Must be secure
- Must be able to perform error checking
- Only need to support iOS 7+
- Preferably more concise code
Here is my current and lengthy code, using several variables, NSMutableURLRequest
, and NSURLConnection
:
dispatch_queue_t queue = dispatch_queue_create("Q", NULL);
dispatch_async(queue, ^{
NSInteger success = 1;
NSURL *url = [NSURL URLWithString:MYURL];
NSData *postData = [[[NSString alloc] initWithFormat:@"method=methodName&username=%@&password=%@", usernameVar, passwordVar] dataUsingEncoding:NSUTF8StringEncoding];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPBody:postData];
NSError *requestError = [[NSError alloc] init];
NSHTTPURLResponse *response = nil;
NSData *urlData = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&requestError];
//if communication was successful
if ([response statusCode] >= 200 && [response statusCode] < 300) {
NSError *serializeError = nil;
NSDictionary *jsonData = [NSJSONSerialization
JSONObjectWithData:urlData
options:NSJSONReadingMutableContainers
error:&serializeError];
success = [jsonData[@"ERROR"] integerValue];
if (success == 0) {
//success! do stuff with the jsonData dictionary here
}
else {
//handle ERROR from server
}
}
else {
//handle error for unsuccessful communication with server
}
});
Could you help me clean this up to be implemented the most preferred way for modern apps of today?