Developing Hybrid Applications for the iPhone: The iBlog Example Application
By Lee S. Barney
Create an iBlog hybrid application! This chapter shows you how to interact with web services and servers in order to retrieve data.
This chapter is from the Rough Cut version of the book.
Sometimes your application may need to access data in a remote database. Or there may be one or more web services from which it needs data. You may even want to synchronize the data on the phone with data stored remotely.
Hybrid iPhone applications make this easy. As hybrid applications they have full access to the XMLHttpRequest object from within JavaScript. This chapter will show you how to download, display, and store XML data designed for consumption by an RSS feed reader. This example allows you to see how it can be easily extended to retrieve other types of data such as HTML or JSON.
As with the database access discussed in Chapter 3, an easy to use wrapper for the XMLHttpRequest object is provided for you to use. If you wish to understand the code used in the wrapper read the later sections of this chapter. If not, the first sections show you how to start using remote data now.
Section 1: The iBlog Example Application
In Chapter 3 the iBlog application showed you how storing and retrieving data to and from the SQLite database on the phone is done. This chapter uses this same sample application to show you how to interact with web services and servers in order to retrieve data. In the examples/chapter 4 directory of the quickconnectiPhone folder you downloaded from sourceforge.net/projects/quickconnect/ you will find two projects, iBlog.dcproj for Dashcode and iBlog.xcodeproj for Xcode in the iBlog directory. These contain the source code and settings for the iBlog hybrid application.
This application allows the user to interact with their wordpress.com blog if they have one. They can read their existing blog entries and also allows them to mark other wordpress blogs as ‘tracked’.
For each tracked and main blog the entries are downloaded to the iPhone, stored in the database, and made available for reading when the iPhone is not connected to any network. In order to store this type of data on the phone it must first be retrieved from the blog provider.
WordPress has an RSS feed that serves up the ten latest blog entries of any blog they are hosting. While it appears that this feed doesn’t serve up full blog entries it actually does. The iBlog application takes advantage of this to retrieve complete blog entries and store them.
Each of these retrieved RSS feed blog entries is stored as a record in the blog_posts table of the iBlogDB database.
Thankfully all RSS feeds are client agnostic. They don’t care what the client is. A feed gets a request for blog postings and sends them out as XML regardless of who or what requests them. The UIWebView, since it contains the WebKit engine, can send requests to the feed and interpret the XML returned. The tool that is used to do this is the XMLHttpRequest object. The methodology to accomplish this is called AJAX.
AJAX is not Greek
One of the greatest written creations of all time is the Iliad. This epic poem by Homer is concerned with the war between the Greeks, also known as the Achaeans, and the Trojans.
One of the greatest heroes of the Greeks is AJAX. Repeatedly he defeats his opponents and in one instance even single handedly saves the Greek fleet from destruction.
Just like the Greek fleet, traditional web page development has been and is under attack. It is viewed as too slow, too hard to use, and inflexible. Once again an AJAX comes the rescue, but this one is not Greek. This AJAX stands for Asynchronous JavaScript and XML.
The concept behind AJAX is simple. Give the user a richer experience by not reloading pages every time they make a request. Instead, send or retrieve data and then use dynamic HTML principles to display the result. All of this can be done within one HTML page by using JavaScript.
AJAX should not be ‘all Greek’ to you.
By combining the XMLHttpRequest object with some simple JavaScript to manipulate a web page your hybrid iPhone application can use remote data just as if it were local data. You then get the best of both worlds. Your application can run in stand-alone mode, networked mode, and can synchronize any data differences when a connection is available. The QuickConnectiPhone framework provides you with an easy to use AJAX wrapper, the ServerAccessObject.




There are no comments for this entry.
[Add Comment]