Data Input Validation using the Windows Azure Mobile Services iOS client SDK

As a follow up to an earlier post, I'm walking through the Windows Azure Mobile Services Tutorials to add in the iOS client version of handling data validation. We're not doing this in the client application (yet); instead, this tutorial, which will appear on windowsazure.com soon, builds up from either the Getting Started With Data or the Using Scripts to Authorize Users iOS tutorials, in that you can use a running application and service from either of those tutorials to get service validation working and handled properly on the client. Then we'll walk through data paging and continue on the tour.

The steps are as follows. You must have either the Getting Started With Data or the Using Scripts to Authorize Users iOS tutorials completed and running. 

  1. In the TodoService.m file, locate the addItem method search for the [self logErrorIfNotNil:error] ; line of code. Beneath that line of code, replace the remainder of the completion block with the following code that checks to see if there was an error in the request and if that error code was –1302, indicating a bad request:

            BOOL badRequest = ((error) && (error.code == -1302));

            // detect text validation error from service.

            if (!badRequest) // The service responded appropriately

            {

                NSUInteger index = [itemscount];

                [(NSMutableArray *)itemsinsertObject:result atIndex:index];

            

                // Let the caller know that we finished

                completion(index);

            }

  2. Build and run; you can see in the Xcode output window that the bad request error from the service was handled:

    2012-10-23 22:01:32.169 Quickstart[5932:11303] ERROR Error Domain=com.Microsoft.WindowsAzureMobileServices.ErrorDomain Code=-1302 "Text length must be under 10" UserInfo=0x7193850 {NSLocalizedDescription=Text length must be under 10, com.Microsoft.WindowsAzureMobileServices.ErrorResponseKey=<NSHTTPURLResponse: 0x755b470>, com.Microsoft.WindowsAzureMobileServices.ErrorRequestKey=<NSMutableURLRequest https://task.azure-mobile.net/tables/TodoItem>}

  3. Finally, in the TodoService.m file, locate the logErrorIfNotNil method, which handles the logging of errors to the output window. Inside the if code block, just below the line NSLog(@"ERROR %@", error); add the following if block:

            // added to display description of bad request

            if (error.code == -1302){

     

                UIAlertView *av =

                [[UIAlertView alloc]

                  initWithTitle:@"Request Failed"

                  message:error.localizedDescription

                  delegate:nil

                  cancelButtonTitle:@"OK"

                  otherButtonTitles:nil

                 ];

                [av show];

            }

  4. Once again, build and run your application, and you should see something like the following behavior:

More coming soon! One thing at a time... now, on to data paging, and we might throw some client-side validation in there, too.

-- Ralph