How-tos

Getting Started with IBM Mobile Application Content Manager for iOS Developers

Share this post:

More and more, the mobile market expects contextualized app content. Retail apps need to provide the user with the most up-to-date catalogs and promotions based on the shopping season, shopper’s location and store inventory. Financial institutions want to promote services best matched to the profile and banking history of individual customers. Governments want to make it easy to keep their citizens informed of current events and deal with emergencies.

But that contextual content puts a big burden on you as a developer. Business owners are coming to you with endless requests for content updates. You spend too much time on the tedious and error prone tasks of content maintenance. These changes tie up development resources that could be doing more valuable work elsewhere.

IBM Mobile Application Content Manager is a cloud-based content management system that empowers the business owner to build and maintain the content while you as a developer can build an app that simply consumes that content. This reduces the maintenance and release burden of the app and reduces the time it takes to get relevant content to the end user.

What you’ll need for this tutorial

  • Apple Macintosh computer running Xcode (this tutorial has been tested with v7.1)
  • Basic familiarity with iOS development
  • A Bluemix ID
  • Sample STARTER iOS app code from GitHub

The STARTER iOS app workspace contains a native Swift iOS app that displays a list of books. When a book in the list is tapped, an image of the book’s cover is displayed. In the starter version, the book data is hard coded in the view controller of the app. Through the course of this tutorial, you will modify the code to leverage the content services of IBM Mobile Application Content Manager (MACM) to retrieve book content from the service dynamically.

“But what if I’m developing something other than a native iOS app?”, you ask. In addition to the iOS SDK that will be explored in this article, there are actually three other methods to access MACM content from client applications:

Also be aware that the SDK for iOS native applications also includes a complete sample app similar to what you will end up with after finishing this tutorial.

Set up your environment

The STARTER iOS app is in a GitHub repository. To download, use the following command:

<code>git clone https://github.com/dschultz-mo/MACM-iOS-Tutorial-STARTER.git
</code>

This tutorial will have you adding code to connect to the MACM server and pull data from it. The code will be included in the body of this tutorial, but if you want to avoid some typing, you can install a few Code Snippets into your Xcode environment. To do so, copy the files from the provided directory into your Xcode environment.

  1. Close Xcode, if it is open
  2. Copy the provided code snippet files into your Xcode user data area using the following command:

    <code>cp MACM-iOS-Tutorial-STARTER/snippets/* ~/Library/Developer/Xcode/UserData/CodeSnippets/
    </code>
  3. Start Xcode by opening the CAAS.workspace file in MACM-iOS-Tutorial-STARTER/. You must open the workspace, not just the individual project. If you do, you will have compilation errors later.

Run the original Books app

The Books app will run on iPad or iPhone and should run on most versions of iOS. It can run on either a physical device or a simulated device. The device or simulator must be able to access internet addresses in order to contact the MACM server you will be creating.

NOTE that there currently is a bug in the app that causes the book list to be hidden if the app starts up in portrait orientation on an iPad. If this occurs, simply rotate the device to landscape and tap on a book. You can then rotate back to portrait mode.

  1. Open the workspace in Xcode if it isn’t already open.
  2. Run the CAASExample app on your device or simulator. The main interface of the app consists of a table view on the left that contains a list of books. As you click on books in the list, the book’s cover is shown in the view area on the right.
    Starter Application Books List
  3. Let’s look at what’s going on in the app. In the Xcode Navigator, open CAASExample/Books/BooksViewController.swift and scroll down to the function getBooks. As you can see the books in the list are simply created with hard coded data within the code of the app.
    Code responsible for the books
  4. If you need to add another book, you must come into the code, edit it, retest the app and redeploy it. See for yourself. Add the following code below the entry for Red October.

  5. Re-run the app.
    Original app with Moby Dick

So here’s the problem… every time the business owners want to add a new book to the list, they have to call up development to make the change. You need to separate the dynamic content from the functionality of the app so that a content owner can update content without involving you. That’s exactly what IBM Mobile Application Content Manager does. Content is developed and managed by content experts (Marketing, Line of Business, Planners, etc.) through a web-based dashboard. The mobile app then uses SDKs to pick up that content dynamically – with no code changes.

Create a MACM service on Bluemix

IBM Mobile Application Content Manager is delivered as a service on the IBM Bluemix cloud. To create a MACM service, you must first have a Bluemix account. If you don’t yet have one, go to http://bluemix.net and click SIGN UP in the upper right corner.

  1. To create your MACM service, follow the instructions https://ibm.biz/BluemixMACMDocs. Go ahead and create your service now.
  2. When you have created your service, click LAUNCH to open the console as a content author.

    This blog post is going to focus on accessing managed content from an iOS mobile app, but it will help to know the basics of how the content is created and managed in the console. For more details on using the MACM console, see https://ibm.biz/MACMKnowledgeCenter.

  3. The tiles on the dashboard each represent a Library. Libraries enable content owners to partition their content. For this tutorial, you will be looking at the Samples library which contains several content items, all of which are of the Content Type Book. Click on Samples to open the library.
    Samples library tile

  4. Each Content Item conforms to its specified template or Content Type. Click on “Adventures of Huckleberry Finn”. Notice the properties and attributes associated with each book.
  5. Leave this page open in your browser. You will come back to it in a few minutes.

Add the CAASObjC SDK to the Xcode workspace

In order to start using MACM, you need to include the SDK into your workspace. To install the CAASObjC SDK into an existing app, add the line pod 'CAASObjC', :git => 'https://github.com/digexp/MACM-SDK-iOS.git' to your Podfile. If you want to use CAASObjC in a Swift application you must also add the line use_frameworks!.

  1. Close Xcode.
  2. Edit your Podfile. It should look like this:
  3. From a terminal opened to the MACM-iOS-Tutorial-STARTER folder, enter the command pod install. Cocoapods will take care of installing the CAASObjC library and any dependencies it needs.
  4. Open the workspace in Xcode again. Now the SDK is available for you to code against.

Replace app hard coded data with CAAS SDK calls

There are basically two things you need to do in the app in order to start pulling content from MACM:

  • Connect to the server
  • Pull content items

Connect to the server

  1. Open CAASExample/CAASExample/AppDelegate. The code changes you need to make are marked with //TODO: markers. The simplest way to navigate around is to use the Jump Bar. Once you have a file open, click on the rightmost item in the breadcrumb in the banner above the editor. You can then select a TODO marker in the list to be taken directly to it. Jump Bar
  2. Go to the first TODO and uncomment the import for CAASObjC.

  3. Force a build by pressing Command-B. If your build didn’t succeed, you either opened the project instead of the workspace or you didn’t get the pod installed correctly.
  4. Go to the next TODO and uncomment the declaration of caasService. The CaasService is the central service you use to get access to the MACM server. It is declared as a global variable so it is visible to all view controllers and supporting files.
  5. Go to the final TODO. It is at this point in the didFinishLaunchingWithOptions function that you want to insert the code to connect to your MACM server. If you installed the Code Snippets like I suggested, just click below the TODO and start typing “TUTORIAL CAAS Connect”. Content assist will offer to insert the snippet for you. The following code is added:

    Let’s break this down a little.

    • CAASNetworkActivityIndicatorManager manages the network activity indicator and listens to all requests coming from the CAASService.
    • The caasService you declared earlier is initialized here. The service takes five arguments which must correspond to the Bluemix service you created earlier. You will customize these in the next step.
    • The app then tests to make sure the connection succeeded.
  6. Customize the baseURL, contextRoot, tenant, username and password values with the values you were given when you created the service. You can find this information on the Mobile Application Content Manager Bluemix dashboard.
    • baseURL – This is the base portion of the URL labeled Published content on the dashboard. It will be something like https://macm-rendering.saas.ibmcloud.com
    • contextRoot – This is the next segment of the Published content URL. It will be wps.
    • tenant – This is the final segment of the Published content URL. In the image below, it is vp4003.
    • username – Use the value for API ID
    • password – Use the value for API password
MACM Bluemix dashboard

Retrieve books data

Now that you have a connection to the server, you can implement the retrieval of dynamic content data. There are two places in the Books app where this is done.

Books View Controller

  1. Open CAASExample/Books/BooksViewController.swift.
  2. Navigate to the first TODO and uncomment the import CAASObjC statement. (Note that you may need to scroll up in the Jump Menu to see it).
  3. Navigate to the second TODO and uncomment the caasService.cancelAllPendingRequests() statement in the refresh procedure. This will cause all pending requests to the service to be cancelled if the user refreshes the screen. It just prevents bad things from happening if a refresh is requested before a previous request finishes.
  4. Navigate to the last TODO which takes you back to the getBooks function you saw earlier. This is where the book inventory is currently hard coded. This is where things will get interesting. You will replace this getBooks routine with a new one that pulls content from MACM.
  5. Delete the function getBooks.
  6. Use the snippet TUTORIALgetBooks to insert the following code:

    Again, the breakdown:

    • An instance of CAASContentItemsRequest is created which will fetch contents from the given contentPath.
    • The completionBlock executes when the content arrives asynchronously and calls the local procedure processReturnData. More on that in a moment.
    • The next several lines configure the contentItemsRequest. These statements tell the request which element and property fields to retrieve and how to sort the returned data. The app really only uses the title and cover properties, but this is an example of how you can request all sorts of information.
    • The geo location data of the device can even be sent with the request, enabling the server to customize returned content based on the location of the user.
    • The request is then executed by the caasService controller.

A couple questions have probably come to your mind at this point.

  • “Where did that contentPath come from?”
    • The first part of the content path Samples/ is the MACM library name. Remember how you opened the “Samples” library in the console earlier?
    • The next section of the path Content Types/ says that you are retrieving content by type.
    • The final section of the path says you are retrieving items of the content type Book.
    • There are several ways to query content. See https://ibm.biz/MACMQueryParams
  • “What elements and properties are available for the contentItemsRequest?”
    • This is determined by how the Book content type has been configured in the MACM server. Go back to your MACM dashboard where you have “Adventures of Huckelberry Finn” open and click the View JSON code button on the left menu. This shows you exactly what data could be sent by the server when a request is made for this content item. The advantage of specifying the elements and properties you want is that you can limit the data traffic to only what is needed by the mobile app.

Now, back to that processReturnData procedure:

  • After some error checking, the response data is iterated upon. For each item, a new Book instance is created. Since this app stores the data locally using CoreData, books are instances of ManagedObjects.
  • The elements and properties of the return data are flattened into a values dictionary and used to fill in the details of the book.
  • Finally, dataController.saveManagedObjectContent is called. All this really does is ask the ManagedObjectController to save the books that were created.

Cover Controller

A URL to the book cover image is contained in the cover property of the book content item. When the user taps on a book in the list, the cover image must be fetched from that URL. In the original app, this is done with a regular NSURLConnection request. Now your images will be coming from the MACM server.

  1. Open CAASExample/PageController/CoverController.swift.
  2. Navigate to the first TODO and uncomment the import CAASObjC statement.
  3. Navigate to the next TODO.
  4. The images in our sample all come from the wikimedia.org server. Delete the declaration of imageHostBaseURL.
  5. Insert the snippet TUTORIALimageHostBaseURL. This will use a property on the caasService as the base URL for image requests.
  6. Navigate to the final TODO in the file. The current downloadImage function uses NSURLConnection to request the image. You want to request the image through the caasService so authentication and security requirements are met.
  7. Delete the downloadImage function.
  8. Insert the snippet TUTORIALdownloadImage to insert the following code:

    The break-down:

    • Cancel any pending requests
    • create a CAASAssetRequest for the given URL. This is similar to a CAASItemsRequest but is specifically for assets such as images.
    • Ask the caasService to execute the request.
    • The completionBlock stops the busy indicator, sets the cover image and unhides it, then adds the new image to the image cache.

Run the Book app

  1. With all the changes complete, Build and Run the app
  2. You should see your app populate with the book content defined on the MACM server now instead of the static books you saw earlier.

Add / change content

Now, let’s say the business owner needs to add that Moby Dick book in again. This time, instead of having you take time away from other tasks to add another book in code to make it happen, your Content Author (Marketing, LoB, etc.) will add the book through the MACM console and the change will be automatically picked up by the mobile app through the use of the MACM SDK.

  1. Return to your MACM Bluemix dashboard.
  2. Open the Samples library by clicking the Samples tile.
  3. Select Create Project from the Project drop-down list at the top of the page. Name the project Moby Dick.
    Create Project menu
  4. Select Book from the Create Content drop-down on the right.
    Create Content
  5. Fill in the attributes of the book:
    Attribute Value
    title Moby Dick
    author Herman Melville
    publish_date 10/18/1851
    cover Open http://upload.wikimedia.org/wikipedia/commons/3/36/Moby-Dick_FE_title_page.jpg in another browser tab, save it to your computer and then Upload
    isbn 978-0553213119
  6. Click Submit for Review. Your new book will appear in the list with a “Draft” decorator. Since it is still Draft, it will not yet appear in the app. (It is possible to also query for Draft content with the SDK. See https://ibm.biz/MACMDraftContent.)
  7. Open the Projects menu.
    Projects menu item
  8. Select the Moby Dick content item and choose Approve from the Actions menu.
    Approve
  9. Provide a comment and click Approve
  10. Click Publish Project

Re-run the app

If all goes well, “Moby Dick” will now appear in your list of books when you run your app.

Summary

This tutorial demonstrates how you can use IBM Mobile Application Content Manager to provide your users with adaptive content, improving their experience with your app. By leveraging adaptive content, you:

  • Put the responsibility of adaptive content where it belongs – in the hands of the content owners
  • Reduce the tedious, time-consuming and error-prone maintenance of content embedded in code
  • Minimize your time to deliver new content
  • Give your users a more personalized mobile experience

Start using IBM Mobile Application Content Manager on your own apps during our open beta period, going on now.

More How-tos stories
May 7, 2019

We’ve Moved! The IBM Cloud Blog Has a New URL

In an effort better integrate the IBM Cloud Blog with the IBM Cloud web experience, we have migrated the blog to a new URL: www.ibm.com/cloud/blog.

Continue reading

May 6, 2019

Use IBM Cloud Certificate Manager to Obtain Let’s Encrypt TLS Certificates for Your Public Domains

IBM Cloud Certificate Manager now lets you obtain TLS certificates signed by Let’s Encrypt. Let’s Encrypt is an automated, ACME-protocol-based CA that issues free certificates valid for 90 days.

Continue reading

May 6, 2019

Are You Ready for SAP S/4HANA Running on Cloud?

Our clients tell us SAP applications are central to their success and strategy for cloud, with a deadline to refresh the business processes and move to SAP S/4HANA by 2025. Now is the time to assess, plan and execute the journey to cloud and SAP S/4HANA

Continue reading