Archive for the ‘ iOS ’ Category

HTTP POST from iOS

This post shares the code to do a simple HTTP POST from iOS.

NSString *post = [NSString stringWithFormat:@"key1=%@&key2=%@",value1,value2];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
NSString *urlString = @"http://someurl";
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

“Expected Expression” when declaring and initializing NSInteger in a switch statement

I ran across a strange problem today. Tried something like this:

switch(controlVariable) {

case 0:

NSInteger x = kSomeConstant;

// some other code…

break;

case 1:

// …

default:

// …

}

And to my surprise, XCode complained saying that “Parse error: Expected Expression”. Upon googling, this is what I found.

So, the solution is to put some statement before you initialize NSInteger. If there is no statement that logically fit, you can simply insert a semi-colon “;” before declaring and initializing the integer.

switch(controlVariable) {

case 0:

;

NSInteger x = kSomeConstant;

// some other code…

break;

case 1:

// …

default:

// …

}

A Tip on programming Table Views

I found a great post on organizing your code while writing code around Table View/Table View Delegate. It details how one can organize sections and rows by making them enums instead of using numbers all over the delegate methods. See for yourself:

Click the post here.

Solved: putpkt: write failed: Broken pipe

If you see this problem, try following these steps.

a) quit Xcode (and the GDB it is running)
b) unplug device
c) delete app from device (hold icon until the ‘X’ appears)
d) turn off device
e) turn on device
f) plug in device (if itunes wants to sync, let it finish)
g) launch Xcode

Xcode Tip: Organize your source code

One can (IMHO should) take advantage of some of the formatting techniques inbuilt (but obscured from the user) in Xcode.  In this post I will discuss two such techniques:

1. Indentation: So you copy-pasted some code from apple’s tutorials and want to reuse it in your source code.  Oh, but wait ! The formatting is all screwed up. You don’t want to indent every line ! Fortunately, XCode provides a hidden trick to reformat your code. All you have to do is copy your entire source code file and delete it and paste it back in. So, Command-A(select all), Command-C (to copy), Command-V(to paste) and BINGO !

2. Organizing methods (using #pragma): With Delegation, a single view controller in your source code can become a delegate of a lot of other view controllers. As a result, it will contain the delegation methods of all those view controllers. Its a good practice to group the delegation methods together and give that a label so that when you view the method list at top of the source editor, the grouped method will show up together under the group header of your choice. This can be achieved by   using the following syntax in source code:

#pragma mark <some label name>
method 1
method 2
.
.
#pragma mark-