Free trial *Internet Service Required

Get started with data in Mobile Services

The iOS client library for Mobile Services is currently under development on GitHub. We welcome feedback on and contributions to this library.

This topic shows you how to use Windows Azure Mobile Services to leverage data in an iOS app. In this tutorial, you will download an app that stores data in memory, create a new mobile service, integrate the mobile service with the app, and then login to the Windows Azure Management Portal to view changes to data made when running the app.

Note

This tutorial is intended to help you better understand how Mobile Services enables you to use Windows Azure to store and retrieve data from an iOS app. As such, this topic walks you through many of the steps that are completed for you in the Mobile Services quickstart. If this is your first experience with Mobile Services, consider first completing the tutorial Get started with Mobile Services.

This tutorial walks you through these basic steps:

  1. Download the iOS app project
  2. Create the mobile service
  3. Add a data table for storage
  4. Update the app to use Mobile Services
  5. Test the app against Mobile Services

This tutorial requires the Mobile Services iOS SDK and XCode 4.5 and iOS 5.0 or later versions.

Note

To complete this tutorial, you need a Windows Azure account that has the Windows Azure Mobile Services feature enabled.

Download the projectDownload the GetStartedWithData project

This tutorial is built on the GetStartedWithData app, which is an iOS app. The UI for this app is identical to the app generated by the Mobile Services iOS quickstart, except that added items are stored locally in memory.

  1. Download the GetStartedWithData sample app from GitHub.

  2. In Xcode, open the downloaded project and examine the TodoService.m file.

    Notice that there are eight // TODO comments that specify the steps you must take to make this app work with your mobile service.

  3. Press the Run button (or the Command+R key) to rebuild the project and start the app.

  4. In the app, type some text in the text box, then click the + button.

    Notice that the saved text is displayed in the list below.

Create mobile serviceCreate a new mobile service in the Management Portal

Next, you will create a new mobile service to replace the in-memory list for data storage. Follow these steps to create a new mobile service.

  1. Log into the Windows Azure Management Portal.
  2. At the bottom of the navigation pane, click +NEW.

  3. Expand Compute and Mobile Service, then click Create.

    This displays the New Mobile Service dialog.

  4. In the Create a mobile service page, type a subdomain name for the new mobile service in the URL textbox and wait for name verification. Once name verification completes, click the right arrow button to go to the next page.

    This displays the Specify database settings page.

    Note

    As part of this tutorial, you create a new SQL Database instance and server. You can reuse this new database and administer it as you would any other SQL Database instance. If you already have a database in the same region as the new mobile service, you can instead chooseUse existing Databaseand then select that database. The use of a database in a different region is not recommended because of additional bandwidth costs and higher latencies.

  5. In Name, type the name of the new database, then type Login name, which is the administrator login name for the new SQL Database server, type and confirm the password, and click the check button to complete the process.

    Note

    When the password that you supply does not meet the minimum requirements or when there is a mismatch, a warning is displayed.
    We recommend that you make a note of the administrator login name and password that you specify; you will need this information to reuse the SQL Database instance or the server in the future.

You have now created a new mobile service that can be used by your mobile apps. Next, you will add a new table in which to store app data. This table will be used by the app in place of the in-memory collection.

Add a new tableAdd a new table to the mobile service

To be able to store app data in the new mobile service, you must first create a new table in the associated SQL Database instance.

  1. In the Management Portal, click Mobile Services, and then click the mobile service that you just created.

  2. Click the Data tab, then click +Create.

    This displays the Create new table dialog.

  3. In Table name type TodoItem, then click the check button.

    This creates a new storage table TodoItem with the default permissions set, which means that any user of the app can access and change data in the table.

    Note

    The same table name is used in Mobile Services quickstart. However, each table is created in a schema that is specific to a given mobile service. This is to prevent data collisions when multiple mobile services use the same database.

  4. Click the new TodoItem table and verify that there are no data rows.

  5. Click the Columns tab and verify that there is only a single id column, which is automatically created for you.

    This is the minimum requirement for a table in Mobile Services.

    Note

    When dynamic schema is enabled on your mobile service, new columns are created automatically when JSON objects are sent to the mobile service by an insert or update operation.

You are now ready to use the new mobile service as data storage for the app.

Update the appUpdate the app to use the mobile service for data access

Now that your mobile service is ready, you can update the app to store items in Mobile Services instead of the local collection.

  1. If you haven't already installed the Mobile Services iOS SDK, install it now.

  2. In the Project Navigator in Xcode, open both the TodoService.m and TodoService.h files located in the Quickstart folder, and add the following import statement:

    #import <WindowsAzureMobileServices/WindowsAzureMobileServices.h>
  3. In the ToDoService.h file, locate the following commented line of code:

    // Create an MSClient property comment in the #interface declaration for the TodoService.

    After this comment, add the following line of code:

    @property (nonatomic, strong)   MSClient *client;

    This creates a property that represents the MSClient that connects to the service

  4. In the file TodoService.m, locate the following commented line of code:

    // Create an MSTable property for your items.

    After this comment, add the following line of code inside the @interface declaration:

    @property (nonatomic, strong)   MSTable *table;

    This creates a property representation for your mobile services table.

  5. In the Management Portal, click Mobile Services, and then click the mobile service you just created.

  6. Click the Dashboard tab and make a note of the Site URL, then click Manage keys and make a note of the Application key.

    You will need these values when accessing the mobile service from your app code.

  7. Back in Xcode, open TodoService.m and locate the following commented line of code:

    // Initialize the Mobile Service client with your URL and key.

    After this comment, add the following line of code:

    self.client = [MSClient clientWithApplicationURLString:@"APPURL" withApplicationKey:@"APPKEY"];

    This creates an instance of the Mobile Services client.

  8. Replace the values of APPURL and APPKEY in this code with the URL and application key from the mobile service that you acquired in step 6.

  9. Locate the following commented line of code:

    // Create an MSTable instance to allow us to work with the TodoItem table.

    After this comment, add the following line of code:

    self.table = [self.client getTable:@"TodoItem"];

    This creates the TodoItem table instance.

  10. Locate the following commented line of code:

    // Create a predicate that finds items where complete is false comment in the refreshDataOnSuccess method.

    After this comment, add the following line of code:

    NSPredicate * predicate = [NSPredicate predicateWithFormat:@"complete == NO"];

    This creates a query to return all tasks that have not yet been completed.

  11. Locate the following commented line of code:

    // Query the TodoItem table and update the items property with the results from the service.

    Replace that comment and the subsequent completion block invocation with the following code:

    // Query the TodoItem table and update the items property with the results from the service
    [self.table readWhere:predicate completion:^(NSArray *results, NSInteger totalCount, NSError *error) 
    {
       self.items = [results mutableCopy];
       completion();
    }];
  12. Locate the addItem method, and replace the body of the method with the following code:

    // Insert the item into the TodoItem table and add to the items array on completion
    [self.table insert:item completion:^(NSDictionary *result, NSError *error) {
        NSUInteger index = [items count];
        [(NSMutableArray *)items insertObject:item atIndex:index];
    
    
    // Let the caller know that we finished
    completion(index);
    
    
    }];

    This code sends an insert request to the mobile service.

  13. Locate the completeItem method, and replace the body of the method with the following code:

    // Update the item in the TodoItem table and remove from the items array on completion
    [self.table update:mutable completion:^(NSDictionary *item, NSError *error) {
    
    
    // TODO
    // Get a fresh index in case the list has changed
    NSUInteger index = [items indexOfObjectIdenticalTo:mutable];
    
    
    [mutableItems removeObjectAtIndex:index];
    
    
    // Let the caller know that we have finished
    completion(index);
    
    
    }];

    This code removes TodoItems after they are marked as completed.

Now that the app has been updated to use Mobile Services for backend storage, it's time to test the app against Mobile Services.

Test the appTest the app against your new mobile service

  1. In Xcode, select an emulator to deploy to (either iPhone or iPad), press the Run button (or the Command+R key) to rebuild the project and start the app.

    This executes your Windows Azure Mobile Services client, built with the iOS SDK, that queries items from your mobile service.

  2. As before, type text in the textbox, and then click the + button..

    This sends a new item as an insert to the mobile service.

  3. In the Management Portal, click Mobile Services, and then click your mobile service.

  4. Click the Data tab, then click Browse.

    Notice that the TodoItem table now contains data, with id values generated by Mobile Services, and that columns have been automatically added to the table to match the TodoItem class in the app.

This concludes the Get started with data tutorial for iOS.

Next steps

This tutorial demonstrated the basics of enabling an iOS app to work with data in Mobile Services.

Next, consider completing one of the following tutorials that is based on the GetStartedWithData app that you created in this tutorial:

Once you have completed the data series, try these other iOS tutorials:

Rss Newsletter