5. iPhone Server Communication

We can communicate in many ways with server. One way is using JSON, this is explained as below:

1. Starting Communication

[[[NSURLConnection alloc] initWithRequest:[NSURLRequest requestWithURL:
[NSURL URLWithString:urlStr]] delegate:self startImmediately:YES] autorelease];
//urlStr is NSString ref

2. Start receiving data

- (void)connection:(NSURLConnection *)connection didReceiveResponse:
   (NSURLResponse *)response{
    [receivedData setLength:0];
}
//receivedData is NSMutableData ref

3. Get data in chunks

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    [receivedData appendData:data];
}

4. After getting complete data

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{

    // json data retrival complete    (received data is NSMutableData)
    if(receivedData == nil || [receivedData length] == 0){
      //handle error in getting response, may be no network
        [self showAlertViewWithTitle:@"No network" andMessage:@"Could not open the
        page, because internet connection was not found." andTag:0];
        return;
    }

    NSString *responseBody = [[[NSString alloc] initWithData:receivedData
      encoding:NSUTF8StringEncoding] autorelease];
    ////NSLog(responseBody);
    NSError *jsonParsingError = [[[NSError alloc] init] autorelease];
    NSDictionary * jsonData = [NSDictionary dictionaryWithJSONData:
                                 [responseBody dataUsingEncoding:NSUTF8StringEncoding]
                                     error:&jsonParsingError];

    NSDictionary *serverError = [jsonData objectForKey:@"error"]==[NSNull null]?
                                   nil:[jsonData objectForKey:@"error"];

    if(serverError==nil || [serverError count]==0){
        // no error found else
        NSDictionary *response = [jsonData objectForKey:@"response"]==[NSNull null]?
                                   nil:[jsonData objectForKey:@"response"];
        NSArray *array = [response objectForKey:@"array"]==[NSNull null]?nil:
                              [response objectForKey:@"array"];
// I have left the code incomplete, its just for understanding

5. Check for null values
Never forget to check null values from JSON data as follows:

NSString *dataValue =  [[[array objectAtIndex:i] objectForKey:@"key"]==[NSNull null]?
                         @"0":[[array objectAtIndex:i] objectForKey:@"key"] intValue]; 

Please let me know if you require further explanation..

4. iPhone Physics Engine Library

We have open source Chipmunk library to implement physics engine in iPhone gaming apps.
Here is the source

This source is the powerful physics 2D engine to use in iPhone game programming. We can have every motion of any real world object in whatever place it is. We can add gravity, friction, external force which will reflect in the motion of the object. And also we can have collision detection between objects present in the current screen.

The source is self explanatory, but I have summarized the usage of this api for handy use and i’ll keep updating this as far as I explore.

rigid bodies: A rigid body holds the physical properties of an object. (mass, position, rotation, velocity, etc.) It does not have a shape by itself. If you’ve done physics with particles before, rigid bodies differ mostly in that they are able to rotate.
collision shapes: By attaching shapes to bodies, you can define the a body’s shape. You can attach many shapes to a single body to define a complex shape, or none if it doesn’t require a shape.
joints: You can attach joints between two bodies to constrain their behavior.
spaces: Spaces are the basic simulation unit in Chipmunk. You add bodies, shapes and joints to a space, and then update the space as a whole.

Please let me know if you want further explanations.