Start your free trial now, and begin learning software, business and creative skills—anytime, anywhere—with video instruction from recognized industry experts.

Start Your Free Trial Now

Reading data from the internet


From:

iOS SDK and SQLite: Building Data-Driven Apps (2013)

with Bill Weinman

Video: Reading data from the internet

Because of the data that we're using comes from the network, we need to create a network connection and read that data before we can parse it and use it. For this, we'll use the NSURLConnection class. Start by making a working copy of BWRSS-XML-02, or in this case, the -done version. You can use your version from the previous lesson if you have it, which would not have this -done at the end. So, I'm going to make a working copy of this, and I'm going to rename it BWRSS-XML-03, and I'm going to go ahead and open the project by double-clicking on this Xcode project file.
Expand all | Collapse all
  1. 8m 29s
    1. Welcome
      57s
    2. Exercise files
      2m 17s
    3. Course overview
      3m 4s
    4. Application overview
      2m 11s
  2. 14m 49s
    1. Prototyping in a testbed
      1m 27s
    2. Building the view controller
      3m 45s
    3. Coding the testbed
      7m 56s
    4. Using the testbed
      1m 41s
  3. 37m 49s
    1. Understanding SQLite in iOS
      1m 41s
    2. Creating an Objective-C interface for SQLite
      9m 57s
    3. Testing the BWDB interface in the sandbox
      7m 1s
    4. Designing a database schema
      8m 7s
    5. Supporting the application with a specific interface
      7m 7s
    6. Using C pointers with automatic reference counting (ARC)
      3m 56s
  4. 21m 18s
    1. Understanding the table view
      1m 33s
    2. Creating the view controller
      6m 39s
    3. Reading from the database
      13m 6s
  5. 33m 50s
    1. Understanding the parsing process
      1m 57s
    2. Creating the item view controller
      12m 25s
    3. Reading data from the internet
      5m 30s
    4. Parsing the feed with NSXMLParser
      8m 2s
    5. Updating the item view with the feed items
      5m 56s
  6. 40m 14s
    1. Understanding the modal view
      1m 47s
    2. Constructing the view controller
      15m 5s
    3. Finding a feed link in a web page
      8m 55s
    4. Parsing the feed with NSXMLParser
      5m 4s
    5. Delegating back to the parent view
      6m 11s
    6. Deleting feeds
      3m 12s
  7. 21m 5s
    1. Creating the web view class
      12m 33s
    2. Coding the web view
      5m 25s
    3. Viewing pages in Safari
      3m 7s
  8. 14m 3s
    1. Understanding the iOS preferences system
      1m 23s
    2. Creating the preferences plist in Xcode
      7m 20s
    3. Reading preferences in your application
      5m 20s
  9. 6m 15s
    1. Adding pull-to-refresh functionality
      2m 34s
    2. Implementing the pull-to-refresh gesture for iOS 6
      3m 41s
  10. 27m 1s
    1. Understanding split view
      1m 4s
    2. Coding the table views
      11m 24s
    3. Implementing the iPad detail view
      6m 35s
    4. Implementing the iPad modal view
      7m 58s
  11. 35s
    1. Goodbye
      35s

please wait ...
Watch the Online Video Course iOS SDK and SQLite: Building Data-Driven Apps (2013)
3h 45m Intermediate Jan 16, 2013

Viewers: in countries Watching now:

The iOS software development kit (SDK) includes the popular SQLite library, a lightweight yet powerful relational database engine that is easily embedded into an application. In this course, Bill Weinman teaches you how to build an RSS reader for iOS devices, integrating XML data and a streamlined interface. He explains how to use the SQLite database, display information in a table view, code view controllers, and create a preferences pane for your app. The resulting application is optimized for all iPhone and iPad displays.

Topics include:
  • Prototyping the app
  • Coding and working with a testbed
  • Creating an Objective-C interface for SQLite
  • Designing a database schema
  • Creating the view controllers
  • Reading and writing to the database
  • Parsing the RSS feed with NSXMLParser
  • Updating the item view with feed items
  • Implementing the pull to refresh gesture for iOS 6
  • Creating a universal application with multiple views
Subject:
Developer
Software:
iOS
Author:
Bill Weinman

Reading data from the internet

Because of the data that we're using comes from the network, we need to create a network connection and read that data before we can parse it and use it. For this, we'll use the NSURLConnection class. Start by making a working copy of BWRSS-XML-02, or in this case, the -done version. You can use your version from the previous lesson if you have it, which would not have this -done at the end. So, I'm going to make a working copy of this, and I'm going to rename it BWRSS-XML-03, and I'm going to go ahead and open the project by double-clicking on this Xcode project file.

We have a working table view here, so now it's time to set up the NSURLConnection class, so we can grab the feed from the network. We're going to start by importing the BWUtilities, because we're going to use that for our error displays. We're going to come back out here to Finder, and we're going to grab a bunch of codes from this 03-NSURLConnection.txt file. This just has a lot of stuff in it that we don't want to have to type right now. We're going to start by grabbing a bunch of these constants.

These constants are used for a number of purposes. We'll take a look at them in a moment here. Copy that and just going to paste all of that right in here at the top of the Source file. This should be right after these imports and right before this class extension for private members. So these constants are used throughout the code for a lot of different purposes, both for the URL connection and for the parsing code, and it's just that we don't have to type a lot of these literal strings and having them all in one place makes it easier to make changes later.

So, this is just good programming practice, to use constants instead of literals. So, I'm going to press Command+S to Save that and come back out here to my text file, and I'm going to grab everything from here all the way to the end. We're just going to paste this in, and we'll look at it as we go through it. So, I'm going to copy, and I'm going to come down here all the way to the end of our code and just before the end marker there, I'm going to paste this all in, and you can see here, starting with our Support methods, we have loadRSSFeed, which creates the NSURLRequest object, and it uses the URL from the feedRecord.

And then it sets up the connection with the delegate as the self, that allows us to use all of these NSURLConnection delegate methods. It sets our networkActivityIndicator that's the little spinner in the bar at the top of the screen on our iOS device. Once that's started, then the NSURLConnection calls these delegate methods, which are again very simple. Set up the connection, we didReceiveResponse, we set that networkActivityIndicatorVisible again, and we set up a place to store the data.

Every time we receive data, we go ahead and appendData that accumulates it in our NSMutableDate object. When we're done, we'll go ahead and detachNewThread to parse the data. Now, we don't have our parser code set up in here yet, so I'm going to comment that out and in the event of an error, I have this didFailWithError, we go ahead and call our error code. So, we have this handlError down here and the errorAlert, which uses the alertMessage method from the BWUtilities, or if we have more data to display, it goes ahead and creates a UIAlertView.

So, in order for this to work, we need to call all this code some place. So, we're going to come back up here to our viewDidLoad--and I'm just going to take out all of these comments here--and I'm going to go ahead and call our loadRSSFeed, self loadRSSFeed, and we will compile, I am pressing Command+B on my keyboard. Build Succeeded. Make sure the iPhone Simulator is selected up here and press Run.

Now when I click on this, you see a little network indicator came up, but we don't have any indication yet that anything is happening. You can see the network indicator kind of flash up there. So, let's go back here in the code, and I'm going to press Command+Period to stop the Simulator. And in our DidFinishLoading in the URLConnection delegate, you notice where I commented out this detachThread for calling our parser that we don't have yet, I'm going to put in an NSLog, so we can see that we got to this point, which means that we have finished loading the data, and we have data.

I'm just going to type have data in the NSLog, press Command+S and press the Run button, and now when I click on one of these, you see we get that have data down here. Just make a little more room for that, and I'll go ahead and click on another one, we have data, and I'll click on another one, we have data. We see our little network indicator coming up there on the Carrier Bar at the top of the Simulator. So, I'm going to switch back to Xcode. Press Command+Period, and we've stopped running the Simulator. Now, our NSURLConnection is complete.

You can see it's pretty simple to use or at least it's a bit demystified. The delegate classes tend to appear less intimidating over time as you get to know them. Now, we are successfully reading the feed using our NSURLConnection object, and we have RSS data that we're ready to parse for the Item View.

Find answers to the most frequently asked questions about iOS SDK and SQLite: Building Data-Driven Apps (2013) .


Expand all | Collapse all
please wait ...
Q: Why is the RSSDB library in the exercise files different than the one in the videos?

A: The RSSDB library had to be updated to work around a bug in the iOS 7 SDK.

There is a bug in the iOS 7 SDK that prevents the BWDB fast enumeration implementation from working on a device. The symptom is code that runs fine on the emulator, but not on a device. iOS devices use an ARM processor, while the emulator runs on your Mac's Intel processor. This points to the LLVM ARM code generator as the source of the bug. Because the bug appears to be in the LLVM compiler, it may be some time before it is fixed.

As a workaround we have changed the getFeedIDs and getItemIDs methods in the RSSDB library so they don't use Objective C fast enumeration. 

Please note that this same bug also affects some of the BWDB testbed code in Chapter 2. The result is that it will run on the emulator but not on a device.

Q: After upgrading to Xcode 5.1 I get an error that says:

"Used type va_list (aka_builtin_va_list) where arithmetic or pointer type is required"

A: Please download the exercise files again to get the latest version of the BWDB library.
Q: I'm using Xcode 6. Why am I getting error messages with the exercise files?
A: A lot has changed in iOS since this course was released. The author is in the process of rewriting the code and updating the course for iOS 8. In the meantime he has prepared a version of the app that works in iOS 8 and Xcode 6. Download it here:
 
http://ios.bw.org/
 
 
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

* Estimated file size

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed iOS SDK and SQLite: Building Data-Driven Apps (2013).

Return to your organization's learning portal to continue training, or close this page.


OK

Upgrade to View Courses Offline

login

With our new Desktop App, Annual Premium Members can download courses for Internet-free viewing.

Upgrade Now

After upgrading, download Desktop App Here.

Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Start your FREE 10-day trial

Begin learning software, business, and creative skills—anytime,
anywhere—with video instruction from recognized industry experts.
lynda.com provides
Unlimited access to over 4,000 courses—more than 100,000 video tutorials
Expert-led instruction
On-the-go learning. Watch from your computer, tablet, or mobile device. Switch back and forth as you choose.
Start Your FREE Trial Now
 

A trusted source for knowledge.

 

We provide training to more than 4 million people, and our members tell us that lynda.com helps them stay ahead of software updates, pick up brand-new skills, switch careers, land promotions, and explore new hobbies. What can we help you do?

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.