Xcode 4.4.1 and iOS 5.1.1 JSON Parse Into Core Data and Display iPad Example

Part of the iOS Category.

Update: There is a new version for Xcode 5.1 and iOS 7.1 here with project and source code files.

It’s not always easy to find simple or fairly simple Xcode example code that only demonstrates one or a few basic concepts. In almost every case, only more complicated examples exist and a beginner can quickly become overwhelmed or lost trying to wade through the complexity.

What beginners need, in my humble opinion, are source code projects demoing only simple concepts as separate projects. There are plenty of complicated and complex examples already out there for more advanced programmers to access.

There are many qualified programmers out there to create simple Xcode templates, some are students. Wouldn’t it be a good idea to have a pool of simple Xcode templates to teach how the basic features of the iOS work? How about these suggestions for starters:

As I figure out the way to implement simple features, I’ll endeavor to create new tutorials here. If I every find a pool of simple up-to-date Xcode projects I’ll link to it in this Category of my blog.

Back in early June 2012, I posted a similar Xcode Tutorial and project Source Code for parsing a JSON file into an iOS app and loading the contents into arrays and dictionaries and then displaying that information on an iPad.

This time, I show how to use Core Data to store the same JSON file content and display it on the iPad2. The Xcode project is based upon the Master-Detail template incorporating ARC, Storyboard, and Core Data options.

The source files unchanged from the default template are:

AppDelegate.h

AppDelegate.m

The source files I modified from the default template are:

DetailViewController.h

DetailViewController.m

MasterViewController.h

MasterViewController.m

MainStoryboard.storyboard

JSON_Parse_And_Display_Core_Data_Example.xcdatamodeld

JSON Classes (folder)

Core Data Classes (folder)

Supporting Files (folder contents added):

  • newbound_logo_text_color.png
  • newbound_logo.png
The idea here is to be able afterwards to code your own Master-Detail template and refer to my modified code to customize your app as you see fit.

More developers and users are using JSON to distribute and acquire data from the Internet. iOS Developers often need to access JSON data and there is a built-in NSJSONSerialization class to retrieve JSON data from an accessible source.

Retrieving JSON data is one thing, putting it to use is another. In looking through Google for examples I found that most of the solutions offered dealt with a very simple JSON data structure of only one-level deep of information. Immediately the question arises how to deal with more complex JSON data structures.

This Xcode 4.4.1 and iOS 5.1.1 app uses CoreData as a way to manipulate the small JSON file. I offer it as an Xcode 4.4.1 project with the source code so you can see how it works under the hood.

The project folder contains the source code and project document along with a sample JSON file. I included the text in case you want to put it on your own web server to test. If you do, you’ll need to update the #define statement to reference the new url. For this project I already have those items hosted on one of  Newbound Inc’s., Amazon Server and the app draws from their associated urls.

When you run the app in Xcode’s 5.1 Simulator and click on the Master button, it will appear as an iPad app in portrait orientation as shown below.

You can rotate the device to landscape and it will appear as below. In the source code you can set this orientation to be the default if you wish.

The Split-View displays a TableView using a custom row header in a grouped style table. Clicking on a row, displays the detail information on the related DetailView.

It’s the source code that’s the most interesting because it shows one way to read a three-level deep JSON file and parse it into different objects and accumulate them into a database managed by Core Data. Understanding how to parse a three-level JSON file means you can handle a n-level JSON file if the need ever arises.

The example shows how to create a custom header using the Grouped Table Style incorporating an image. There are some comments in the code to offer insights. The image when shown in the DetailView will animate using blocks with a typedef. I learned that coding technique from the excellent NSScreencasts paid sessions.

The example is meant as a teaching tool for the aspects I’ve described above. There are other ways to accomplish receiving JSON data and manipulating that into useful information. Other features desired are left as an exercise for the user. 😉

I have included the Xcode 4.4.1 and iOS 5.1.1 project here just in case you want to examine and run the completed application using the iPad Simulator. 🙂

Enjoy!

Did you like this? Share it:

About Don Larson

Using computer technology since June 1980.
This entry was posted in Education, iOS, iOS Tutorials, Native Apps, Newbound, Inc., Technology. Bookmark the permalink.