Online Book Reader

Home Category

iOS Recipes - Matt Drance [70]

By Root 253 0
close];

[self.uploadFileInputStream removeFromRunLoop:[NSRunLoop currentRunLoop]

forMode:NSDefaultRunLoopMode];

self.uploadFileInputStream = nil;

}

Our multipart POST request is now complete. The calling code then creates an NSURLConnection using the prepared request and awaits news of completion. The MultipartHTTPPost project reviews how this whole process works from start to finish.

MultipartHTTPPost/iPhone/AppDelegate_iPhone.m

- (void)upload:(id)sender {

NSString *URLString = @"http://localhost:3000/upload";

self.request = [PRPMultipartPOSTRequest

requestWithURL:[NSURL URLWithString:URLString]];

self.request.HTTPBoundary = @"d0ntcr055t3h57r33m2";

NSMutableDictionary *params;

params = [NSMutableDictionary dictionaryWithObjectsAndKeys:

@"Tyler", @"name",

@"Soap salesman", @"occupation",

nil];

self.request.formParameters = params;

NSString *imgFile = [[NSBundle mainBundle]

pathForResource:@"pic" ofType:@"jpg"];

[self.request setUploadFile:imgFile

contentType:@"image/jpeg"

nameParam:@"filedata"

filename:@"uploadedPic.jpg"];

PRPBodyCompletionBlock completionBlock;

completionBlock = ^(PRPMultipartPOSTRequest *req) {

NSLog(@"Completion Block!");

NSURLResponse *response = nil;

NSError *error = nil;

NSData *responseData;

responseData = [NSURLConnection sendSynchronousRequest:request

returningResponse:&response

error:&error];

if ([responseData length] > 0) {

NSLog(@"Upload response: %@",

[NSString stringWithCString:[responseData bytes]

encoding:NSUTF8StringEncoding]);

} else {

NSLog(@"Bad response (%@)", responseData);

}

};

PRPBodyErrorBlock errBlk = ^(PRPMultipartPOSTRequest *req,

NSError *error) {

NSLog(@"ERROR BLOCK (%@)", error);

};

[self.request prepareForUploadWithCompletionBlock:completionBlock

errorBlock:errBlk];

}

Like the basic POST recipe, this project includes a simple WEBrick servlet to test this code.[7] To run it, navigate to the directory containing webserver.rb and type ruby webserver.rb. You should see some output indicating the server has started. To verify the server is running, drag the local form.html file into a browser, fill out the form, submit it, and check the response. Once you’ve verified the static HTML form works, move on to verify the MultipartHTTPPost project.

Between this recipe and the earlier basic POST recipe, you should be well prepared to support a variety of web service uploads.


Footnotes

[4] Special thanks to Mike Clark for contributing this recipe’s test server code.

[5] http://tools.ietf.org/html/rfc1867

[6] http://www.w3.org/Protocols/rfc1341/7_2_Multipart.html

[7] Special thanks to Mike Clark for contributing this recipe’s test server code.


Copyright © 2011, The Pragmatic Bookshelf.

Chapter 5

Runtime Recipes

These recipes cover a broad range of iOS topics including UIKit, Core Data, and the Objective-C runtime. They illustrate techniques that can help you gather information during an inevitable troubleshooting session and bring order to potentially chaotic areas of your project, or they introduce you to newer features of the Objective-C language that you may not be using. In all cases, the goal is to help you be a smarter, more efficient iOS developer.

Recipe 35 Leverage Modern Objective-C Class Design

Problem

Objective-C has a long history and an extremely deep set of frameworks. The language’s traditionally verbose nature can lead to noisy header files and implementations that are hard to read or maintain. How do you keep your interfaces clean and readable without compromising functionality?

Solution

Many of the recipes in this book take advantage of recent developments in Objective-C and Clang to keep the code lean and readable. It’s worth discussing these techniques up front so you can understand their motivations and ideally find ways of using them to make your own projects easier to manage and maintain.

Let’s start with a simple Cocoa class representing a book that defines some private, read-only data wrapped in properties. It’s a contrived example but sufficient for the goals of this

Return Main Page Previous Page Next Page

®Online Book Reader