Sunday 11 January 2009

Sending HTTP request from Iphone App

How to send a HTTP request from Cocoa Application
        //prepar request
	NSString *urlString = [NSString stringWithFormat:@"http://urlToSend.com"];
	NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
	[request setURL:[NSURL URLWithString:urlString]];
	[request setHTTPMethod:@"POST"];
	
        //set headers
	NSString *contentType = [NSString stringWithFormat:@"text/xml"];
	[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

	//create the body
	NSMutableData *postBody = [NSMutableData data];
	[postBody appendData:[[NSString stringWithFormat:@"<xml>"] dataUsingEncoding:NSUTF8StringEncoding]];
	[postBody appendData:[[NSString stringWithFormat:@"<yourcode/>"] dataUsingEncoding:NSUTF8StringEncoding]];
	[postBody appendData:[[NSString stringWithFormat:@"</xml>"] dataUsingEncoding:NSUTF8StringEncoding]];
	
        //post
	[request setHTTPBody:postBody];
	
	//get response
	NSHTTPURLResponse* urlResponse = nil;  
	NSError *error = [[NSError alloc] init];  
	NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];  
	NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
	NSLog(@"Response Code: %d", [urlResponse statusCode]);
	if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) {
		NSLog(@"Response: %@", result);
	        
                //here you get the response

	}

Friday 9 January 2009

Getting data from info.plist

How to get data from info.plist file in Iphone app development :
NSString * stringFromInfoPlist = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"stringInInfoPlist"];



Update : More info on *.plist files management here : iPhone Development Blog

Wednesday 7 January 2009

A little meal at office


via Thomas Nicholls

Saturday 3 January 2009

Getting contacts from adress book

Here is a simple to way to access to Iphone address book for Cocoa Iphone developers :
// open the default address book.
ABAddressBookRef m_addressbook = ABAddressBookCreate();
if (!m_addressbook) { 
    NSLog(@"opening address book"); 
}

//get people and index
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(m_addressbook);
CFIndex nPeople = ABAddressBookGetPersonCount(m_addressbook);

//iterate on each person
for (int i=0;i < nPeople;i++) {
    ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);

    //get phone numbers and names
    ABMultiValueRef phoneNumberProperty = ABRecordCopyValue(ref, kABPersonPhoneProperty);
    NSArray* phoneNumbers = (NSArray*)ABMultiValueCopyArrayOfAllValues(phoneNumberProperty);
    NSString* name = (NSString*)ABRecordCopyValue(ref, kABPersonFirstNameProperty);
    CFRelease(phoneNumberProperty);

    //Loop to display results
    NSLog(@"Name = %@", name);
    NSLog(@"Phone numbers = ");
    for (int j=0; j<[phoneNumbers count]; j++) { 
        NSLog(@" phone -> %@", [phoneNumbers objectAtIndex:j]); 
    }

    //release memory
    [phoneNumbers release];
}