Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.
    + (void) makeRequestToEndPoint:(NSString *) endpoint values:(NSMutableDictionary *) params onCompletion:(SDKCompletionBlock) responseHandler
    {
        NSString * urlString = [self createApiUrlFromEndpoint: endpoint];
        NSURL * url = [NSURL URLWithString: urlString];

        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: url];
        request.HTTPMethod = @"POST";
        [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [request setValue:@"charset" forHTTPHeaderField:@"utf-8"];
        [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        request.HTTPBody = [[params urlEncodedString] dataUsingEncoding:NSUTF8StringEncoding];

        [NSURLConnection sendAsynchronousRequest:request
                                           queue:[NSOperationQueue mainQueue]
                               completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
        {

            NSDictionary * dictionary = nil;
            NSError * returnError = nil;
            NSString * errorCode = nil;
            NSString * errorText = nil;
            NSInteger newErrorCode = 0;

            if([data length] >= 1) {
                dictionary = [NSJSONSerialization JSONObjectWithData: data options: 0 error: nil];
            }

            if(dictionary != nil) {

                 if([dictionary objectForKey: @"error_code"] != nil) {
                     errorCode = [dictionary objectForKey: @"error_code"];
                     errorText = [dictionary objectForKey: @"error_description"];
                 }
            }

            NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];

            if(statusCode >= 400)
            {
                if(errorCode == nil)
                {
                    newErrorCode = -1;
                    errorText = @"There was an unexpected error.";
                }
                else {
                    newErrorCode = [errorCode intValue];
                }

                NSMutableDictionary* details = [NSMutableDictionary dictionary];
                [details setValue: errorText forKey:NSLocalizedDescriptionKey];

                returnError = [NSError errorWithDomain: WPAPPErrorDomain code: newErrorCode userInfo: details];
            }

            responseHandler(dictionary, returnError);

        }];

    }

I have the code above in my IOS SDK that I am building that makes it easy for users of the SDK to make API calls.

Basically users can specify an API endpoint and easily make API calls.

Is there any way to improve the code above to make it more maintainable and easy for users?

Is that the proper way to handle asynchronous networking in IOS?

share|improve this question
add comment

1 Answer

This code is nice for asynchronous web service calls. Instead of writing this method every controller of your project, I would suggest writing down this method in another class such as ServerHandler. This class may declare one protocol which will have 2 delegates.

(void) didReceiveResponse:(NSDictionary *)resposneDict;

(void) failedToReceiveResponseWithError:(NSError *)error;

As per you status code in your code, you can check which delegate needs to be called. Both of these delegates will be overridden in the sender controller which is requesting a web service.

It would help you not to write down this code everywhere in your app. Making the code more modular would help as well.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.