This basic example will show you how to get submit data to a JotForm form from an iOS app. JotForm makes it extremely quick easy to setup a form, and it provides all the backend support you need to manage that data. If you have a need to submit some data from an iOS app back to you it is ideal, especially if you can stay under the 100 submissions a month limit for a free account. I am looking at all the ‘contact us’ and submit a question screens out there.
To get started you will need to create a form using JotForm’s web builder. One great bit of UX is the fact you can do this without signing up for an account.
I have setup a simple contact form, with first name, last name and a comments field.
To see the form details you will need to load a sample of the form. You do this by going to the ‘My Forms’ button at the top of the page, selecting your form and choosing the View Form from the more button on the resulting page.
There will be a couple of bits of key information that you need to gather from your sample form. The first is the formID, you will see that as a 14 digit number in the URL. Then you will need to get the field ids. Do this by opening up the source and looking name fields on the inputs.
With these details we are able to construct a request to send to JotForm from an iOS app. I will leave the form creation up to you, but if you need a hand with form creation have a look at the EZ Form ios library. To make the submission easier I am using AFNetworking Library.
NSURL *url = [NSURL URLWithString:@"http://submit.jotform.co"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSDictionary *modelData = [self.emailForm modelValues];
NSDictionary *params = @{@"q1_firstName": self.firstName.text,
@"q4_lastName": self.lastName.text,
@"q3_yourComments": self.comments.text,
@"simple_spc":@"32096649753869-32096649753869",
@"formID":@"32096649753869"};
[httpClient postPath:@"/submit/32096649753869/" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *responseStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"Request Successful, response '%@'", responseStr);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"[HTTPClient Error]: %@", error.localizedDescription);
}];
There are a number of spots where the formID gets copied, both in the URLs and in a number of fields. I included the simple_spc field with the formID repeated to mimic the JotForm javascript API. Then with AFNetworking I submit the request and simply log the success and failure blocks.
The only time that the JotForm api differs is when you submit a file with your form submission. In this case the form changes from simple parameters to a multi-part encoded submission. In that case you will need to create NSMutableURLRequest:
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/submit/32096649753869/" parameters:nil constructingBodyWithBlock: ^(id
[formData appendPartWithFileData:UIImageJPEGRepresentation(picture) name:@"q5_photo" fileName:@"photo.jpeg" mimeType:@"image/jpeg"];
[formData appendPartWithFormData:[@"32096649753869" dataUsingEncoding:NSUTF8StringEncoding] name:@"formID"];
[formData appendPartWithFormData:[self.firstName.text dataUsingEncoding:NSUTF8StringEncoding] name:@"q1_firstName"];
[formData appendPartWithFormData:[self.lastName.text dataUsingEncoding:NSUTF8StringEncoding] name:@"q4_lastName"];
[formData appendPartWithFormData:[self.comments.text dataUsingEncoding:NSUTF8StringEncoding] name:@"q3_yourComments"];
[formData appendPartWithFormData:[@"32096649753869-32096649753869" dataUsingEncoding:NSUTF8StringEncoding] name:@"simple_spc"];
}];
Then the submission is handled by creating a AFHTTPRequestOperation and setting the success and failure handlers. Once they are set it is then a matter of calling [httpClient enqueueHTTPRequestOperation:operation]; and the request will be submitted.
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Response: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Failed with error: %@", error);
}];
[httpClient enqueueHTTPRequestOperation:operation];
If you want to keep track of your form submission process you can setup a progress block to inform you as bytes are being written.
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];
Leave a Reply