Question

I'm trying to make my own Request class I intend to use throughout my app. Here is the code I've been coming up with so far.

-(IIWRequest *)initAndLaunchWithDictionnary:(NSDictionary *)dictionnary
{
    self=[super init];
    if (self) {
        // Create the request.
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://xxxxx.com/app/"]];

        // Convert data
        SBJsonWriter *jsonWriter = [[SBJsonWriter alloc] init];
        NSString *jsonData = [jsonWriter stringWithObject:dictionnary];
        NSLog(@"jsonData : %@",jsonData);
        NSData *requestData = [jsonData dataUsingEncoding: NSUTF8StringEncoding];
        request.HTTPBody = requestData;

        // This is how we set header fields
        [request setHTTPMethod:@"POST"];
        [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        [request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
        [request setHTTPBody: requestData];

        // Create url connection and fire request
        NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
        [self activateNetworkActivityIndicator];
        if (connection) {
            NSLog(@"Connection");
        } else {
            NSLog(@"No connection");
        }
    }
    return self;
}

I have included NSURLConnectionDelegate. I'd like to fire the connection callbacks such as did finished or did fail back to the function mentioned before. The goal of all that is to get only one method to call in the end looking like :

-(IIWRequest *)initAndLaunchWithDictionnary:(NSDictionary *)dictionary inBackgroundWithBlock:^(BOOL succeeded){}

Any idea ? Thanks !

Was it helpful?

Solution

Use block method of NSURLConnection class it will reduced your functionality as well sendAsynchronousRequest:queue:completionHandler:

Read this doc.

OTHER TIPS

I would hardly suggest you to use one of the currently existing libraries for calling URLs. One of the best I know is AFNetworking https://github.com/AFNetworking/AFNetworking. There is lot of examples and its easy to use and I am sure you should go with it.

Anyway, if you want to build your own class I would suggest you to read post written by Kazuki Sakamoto here NSURLConnection and grand central dispatch.

Regards

If you are using the iOS 7, I recommend A LOT you to use NSURLSession classes, this new network api is really amazing and simple.

Anyway, to answer your question, you just need to hold the reference of callback in your class and call it when you receive some response from the server.

To hold the reference, you can do something like this:

// in your .h file
typedef void (^ResponseBlock)(BOOL success);

// in your .m, create a class extension and put declare the block to use it for callback
@interface MyClass ()
{
    ResponseBlock callback;
}

// You can store reference using equal like this
- (void)myMethodRequestWithResponseBlock:(ResponseBlock)responseBlock
{
    callback = responseBlock;
    // statements
}

// And finally, you call back block simple like this:
callback(success);

Again, use NSURLSession api if you can, you will simplify your work.

I hope this may help you. Cheers!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top