-1

I am trying to build a Survey application where the surveys will be taken offline on multiple ipads and when these ipads are online they are going to upload the data(survey answers) to our servers? I am really struggling how to send the survey to multiple ipads and more importantly to capture from different ipads to one source ?

I need help to clear my architecture part and I need some examples to do the coding part. Do you know anything similar?

What are you ideas?

Many Thanks in advance, Arda

  • 1
    Please clarify your needs. You really need to send data (from server) to the application, or can the app load new surveys on startup? What is your problem with sending the results back to the server? Sounds like a simple data upload. – ottel142 May 23 '13 at 12:07

1 Answers1

1

Create a web server to accept and send survey questions and answers.

I would envision an app that goes like this:

1) Slave iPads makes a HTTP POST request to server asking for the survey

This is usually done using a networking library for iOS like MKNetworkKit or AFNetworking. The general process is to:

  • create a NSDictionary of key-value pairs to form the HTTP POST request
  • submit the data through a block construct with completion handler

So something like:

MKNetworkOperation *op = [engine operationWithURLString:@"http://www.mywebserver.com/api/fetchQuestions"
                                                 params:nil
                                             httpMethod:@"POST"];

2) Server receives request, grabs all survey questions in database and return JSON encoded questions to slave ipads.

I'm not sure what platform your web server is on but in the past, I used Symfony 2.0 which is a PHP web framework.

It provides very helpful tools like Doctrine (an Object Relational Mapper or ORM) to let me work with my MySQL data as if they're programming objects.

So my general process for fetching data would be something like:

// pseudo php function codes
public function sendSurveyQuestionAction()
{
    $repository = $this->getDoctrine()->getRepository('MyAppBundle:Survey');
    $query = $repository->createQueryBuilder('query')->getQuery();

    $arrObjs = $query->getResult();

    $arrObjDatas = NULL;

    foreach($arrObjs as $obj)
    {
        $arrObjDatas[] = $obj->toArray();
    }

    $response = new Response(json_encode(array('data' => $arrObjDatas)));
    $response->headers->set('Content-Type', 'application/json');

    $return $response;
}

This would return all survey in JSON format, ready to be parsed by your master iPad app.

3) Users on slave iPads fill in the questions through the app UI and submits. The app saves the data to disk, checks for a working internet connection before sending data back to server.

Submitting the answer is very similar grabbing the questions, so your iOS code should be something like:

// ------------------------------------------------------------------------------------
// store all question-answers into a dictionary to be submitted as HTTP POST variables
// obviously, you wouldn't create it here, this is just example code, you would likely
// have stored your questions and answers when user presses 'finish' button
// ------------------------------------------------------------------------------------
NSMutableDictionary *paramDictionary = [[NSMutableDictionary alloc] init];
[paramDictionary setObject:@"5" forKey:@"q1"];
[paramDictionary setObject:@"10" forKey:@"q2"];
[paramDictionary setObject:@"15" forKey:@"q3"];

// this helps your web server know how many question-answers to expect, or you could hard code it into your business logic
[paramDictionary setObject:[NSNumber numberWithInteger:3] forKey:@"numberOfQA"];

MKNetworkOperation *op = [engine operationWithURLString:@"http://www.mywebserver.com/api/submitAnswers"
                                                  params:paramDictionary
                                                  httpMethod:@"POST"];

This will submit your answers for each of your question. You may have noticed I used q1, q2, q3.

These are for your web server code to identify each questions and extract the respective answers from them.

4) Server receives finished answers and commit them to database

So if you were using Symfony 2.0 PHP code, then something like:

// pseudo php function
public function saveAnswersAction()
{
    $numOfQA = $_REQUEST['numberOfQA'];

    for($i = 0; $i < $numOfQA; $i++)
    {
        // ----------------------------------------------------------------------
        // looping through all the questions such as q1, q2, q3, q4, q5....
        // by appending the counter variable to the question identifier
        // ----------------------------------------------------------------------
        $currentAnswer = $_REQUEST['q'.$i];


        // use Doctrine to create new answer entities, and fill in their data
        $answerEntity.answer = $currentAnswer;

        $surveyEntity->addAnswerEntity($answerEntity);

        // mark survey as complete so we can fetch all 'completed' surveys later
        $surveyEntity.complete = true;
    }

    // tell Doctrine to commit changes to MySQL Database

    // return HTTP OK status message
}

5) Now all that's left is for your master iPad app to make a HTTP POST request to get all surveys.

The process is the same with your iOS code making a HTTP POST requesting for all 'completed' survey entities from your web server.

The web server grabs them and return them as JSON encoded data.

Your app then receives the completed surveys with question answer like this:

surveys
{
    {
        questionNumber: 1,
        questionAnswer: "5"
    },
    {
        questionNumber: 2,
        questionAnswer: "10"
    },
    {
        questionNumber: 3,
        questionAnswer: "15"
    }
}

Now you use JSONKit to parse this JSON data. You should end up with a NSDictionary from JSONKit.

You can then go something like:

// pseudo code
-(void)displayCompletedSurveys
{
    [MKNetworkOperationEngine doRequest:
                              ...
    ^completionBlock {
        // parse JSON data
        NSDictionary *surveyData = [JSONKit dictionaryFromJSONData:data)

        NSEnumerator *enumerator = [surveyData enumerator];
        NSDictionary *currentQuestion = nil;

        while([enumerator nextObject] != nil)
        {
            // do something with each of your question-answer e.g. show it on screen
        }
    }];
}

Points To Consider

Most of the code above are pseudo-codes. Your final real code would probably be much more in depth.

You'll need to build some master login into your app to prevent everyone from seeing the completed surveys.

Some Extra Information You Should Know

Here are some extra information to help you

  • JSONKit for fast JSON data decoding from your web server
  • MKNetworking or AFNetworking to submit your data to your web server
  • You need to know how to write web services to handle accepting the survey answers. I recommend learning a web framework like Symfony 2.0

Hope that helps.

Zhang
  • 11,549
  • 7
  • 57
  • 87