Top of the page
Close

This is a red error

This is a red error

This is a red error

Building custom forms in your iPhone app using iMessages, ASIHTTPRequest and json-framework

Ondrej Rafaj on 2010.02.04 12:06:49

Attached files
iMessages_tutorial.docx Download
iMessages.zip Download

Hi, today I want to show you how to use our brand new API that allows you to create contact / feedback forms in matter of minutes. In this tutorial we will be using JSON-framework and ASIHTTPRequest. Both are open projects and you can use them in your apps without any limitations.

In this tutorial you will actually learn how to send POST request to the website using the ASIHTTPRequest and you can see a basic usage of the JSON-framework.

Lets start with our xProgress back-office. If you don’t have an account already, register yourself on the server (http://www.xprogress.com/) , go to the My Account section, click on iMessages.

Fill the name of your new form application and setup up to five recipients who will receive data from the form. All the data is stored in the same time in the database than you don’t have to worry about loosing anything if there will be a problem with your email.

Once you have your first form created, you can manage the fields you want to use … click on Manage Fields in the table above the form.

On this page you can manage and reorder the fields you want to have in the form.

Once you have those fields set-up, click the Back button and click on those little gears, just next to the name of your form. This link will take you to the API example page which contains your unique id hash code that has to be included in your code and the example code (same one I’ve used in the example application in this tutorial.

This is the main function that is sending your data:


- (IBAction)sendMessage:(UIButton *)sender {
	NSURL *urlToSend = [[[NSURL alloc] initWithString: @"http://www.xprogress.com/imessages-send/?code=e179922a111ee2cbb6f09d85e84c8e2d2f026323"] autorelease];
	
	ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:urlToSend] autorelease];
	
    [request setPostValue:[self.fieldName text] forKey:@"name"];
	[request setPostValue:[self.fieldSurname text] forKey:@"surname"];
	[request setPostValue:[self.fieldMail text] forKey:@"email"];
	[request setPostValue:[self.fieldSubject text] forKey:@"subject"];
	[request setPostValue:[self.fieldMessage text] forKey:@"message"];
	
  	[request start];
	NSError *error = [request error];
	if (!error) {
		NSString *response = [request responseString];
		SBJSON *parser = [[SBJSON alloc] init];
		NSDictionary *data = [parser objectWithString:response error:nil];
		[parser release];
		int result = [[data objectForKey:@"result"] intValue];
		if (result) self.resultLabel.text = @"Your message has been sent.";
		else self.resultLabel.text = @"We were unable to send your message!";
		NSString *error = [data objectForKey:@"error"];
		if ([error length] > 0) NSLog(@"Error: %@", error);
	}
}

Now you just have to setup all the fields in Interface Builder (IB) and the work is done :) Full example is included and is available for download in the top-right corner of this article.

If you’ll have any questions please feel free to contact me anytime, using the comments section bellow this tutorial, I’ll get the text message straight away ...

Remember that once you are registered you can submit your code snippets (working on tutorial and application submission as well).

Anyway, thanks for your time and I hope that you’ll come back soon,

Ondrej

Ondrej Rafaj

Ondrej Rafaj

Technical director @ Fuerte International

Back to top Comment

Comments ... Why not to get involved!
HTML Comment Box is loading comments...

Old comments:

Back to top

IE SUCKS, DON'T USE IT :) Digg this page