Tell me more ×
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

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.