TweetFollow Us on Twitter

Building the Internet-Connected Application

Volume Number: 19 (2003)
Issue Number: 1
Column Tag: Mac OS X

Building the Internet-Connected Application

Integrating the Internet into your Ideas

by By Dan Wood

Introduction

You may have noticed that it's almost impossible to run an application on Mac OS X without it trying to connect to the Internet to do something. The operating system checks for updates to itself. iTunes queries a database when you insert a music CD. OmniGroup's applications have a menu that says "Check for updates." The list goes on.

Taking advantage of the fact that many Mac OS X installations have a permanent (or frequent) Internet connection is something that you can have your own application do as well, even if it's not primarily categorized as an "Internet Application." The purpose of this article is to give you some motivation to do so, and some hints about making it a good experience.

Advantages of a Well-Connected Application

So what are some of the benefits of an application that connects the Internet? Here are just a few:

  • You can make sure that your users are aware of new versions of your program being available. This is a great boon for support, because you will have to deal with users of older versions of your application less frequently if they are spoon-fed the newest version.

  • Individual components of your application--plug-ins, templates, etc.--can be listed and downloaded directly from the application if you maintain a repository of these items. Watson, for example, allows users to download and install additional plug-ins directly from the program, rather than having to go hunt for them all over the Internet.

  • Data about the user (what version of the application and OS she is running, for example) can be collected by a server.

Dumb Servers

OK, so you think it's a good idea to have a server going for your desktop application can connect to, but what if you aren't a "server-side" kind of person? Are you going to have to learn new technologies like WebObjects or PHP? Will you need to buy an XServe and host at an expensive colocation facility?

That depends. You can actually accomplish quite a bit without any server-side programming, and very basic Web deployment capabilities. How's that? Just keep it simple, and put all of your logic on the client --the desktop program -- and only data on the server.

Imagine the following scenario: You want to have your program connect to your server and determine if a new version of the application is available, so you can inform the user that they need to fetch the newest version. One approach is to send a message to your server and include the version of the application in that message. The server would then compare that value with the known latest version, and then either send back a message saying that the application is up to date, or a message saying that the user needs to upgrade. This, of course, would require programming on the server.

What if, instead, the client application just connected to the server and asked for the latest application version number. The client would then compare its own version with the number it received, and then decide whether or not to present an alert about application update availability.

This is the fundamental difference between a "smart" and a "dumb" server. The dumb server can merely be a static file that is served by your Web-hosting site of choice. If you're a Cocoa or Carbon programmer, you don't need to write code in an unfamiliar environment and find an Internet hosting service to host that server-side program for you.

Obviously, the Dumb Server approach will only go so far; some transactions are going to require logic on the server to react to parameters sent by the user. But here are some items that can be put into a static file on the server, that your client program can react to.

  • Current application version number, as discussed above. The client compares it to its own version and presents a message, accordingly.

  • URL of where to fetch the new version of the application. If an update is needed, the client could download the update directly.

  • Description of new features. To motivate the user into downloading the upgrade, you could provide a small list of new features.

  • Data that may be needed later in the execution of the application. If your program allows the user to view a list of online downloadable modules, templates, or documents, you could provide a list of all available files along with the other information, and tuck it away until it is actually needed. Such a list could also contain version numbers, so your application could make sure it has the most up-to-date versions of the available components.

  • News and status. There's an interesting way to stay connected with your user base, by providing a bit of news that might display each time the application is launched. It could be a "tip of the day" that you maintain on the server; it could be a holiday greeting, it could be links to relevant Web sites.

You could even provide different variations of the information you provide, so users running your program in "demo" mode will view one type of information, while your paid users get different data. Or your data could depend on the OS version used so Mac OS X 9 users will see one message while Mac OS X users get a different one. You could either put all variants into a single file, and let the application pick out the appropriate piece; or you could have the application access different URLs depending on that status. For example, you might serve up two files on your site, and your application would fetch the appropriate one.

If your application is to be localized into different languages, you could also localize your messages as well. The client would request a file based on the user's primary preferred language (e.g. "hello_fr" for a French version). If that file doesn't exist, the client would then try again with the secondary language. If you don't think you'll be constantly adding new languages, you might want to put a limit on the number of attempts for your client to try, and store the best available language as a preference for the next time through.

Smart Servers

So you think you need a little bit of logic on the server? Welcome to the world of client-server programming. You will need to determine just how much logic is going to go into your server and what kind of software and hardware you will need to run it. Unless you are building an application with high bandwidth and database needs, you can probably get by with a simple setup that is hosted on a service that costs about $10 per month and provides server-side capabilities such as PHP and MySQL or Postgres. Macintouch has some useful links at the following URL: http://www.macintouch.com/dotmacalt.html. You could get more sophisticated with WebObjects or other technologies, deployed on colocated hardware, but that is way beyond the scope of this article.

A smart server has a number of advantages; it can provide whatever information is appropriate to the parameters given in a request; if it is told your operating system, language, registration status, etc., it can deliver exactly the message you wish to deliver. It can deliver personalized, targeted information, as well. For example, your client application could send a request for a "Web postcard" to be sent, and provide the recipient's name and email address; your server could build up an image and email the postcard. This wouldn't be possible without some logic on the server.

User Interaction

If you are building Internet connectivity into your application, you need to make sure that the program behaves nicely. If the user doesn't have network connectivity, they might just be on a PowerBook, or they may have chosen to disable net access, perhaps if they are on a dialup connection. Just fail gracefully; you will probably have the opportunity to connect to the server later. Your program shouldn't nag the user too much if a new version is available; allow the user to stop telling you about the updates, or only show the reminder every so often. And don't initiate big downloads without the user's permission. They may be on a slow connection, and prefer to download things later.

Getting User Data

It may be useful for you to pay attention to usage patterns of your application. Either for practical purposes, or if you are just curious, it can be useful to know something about your users, such as what version of the application they are using, or what version of the OS they are on. A "smart" server could take parameters passed from the server and add those data to a database. Even if you're just using a dumb server, you could perform some analysis on your web logs.

Be careful about privacy concerns! It is possible to have your client read user information (name, email) from the system settings, get addresses and phone numbers out of the address book, upload files in the user's home directory, and all sorts of unspeakable acts. The users of your program need to trust that you aren't writing "spy-ware." You should be liberal with the privacy statements in your program, and if your program does upload any information about individual users, you should be very clear about how that information is used and stored.

Anticipating Future Needs

In creating your interaction model between your client and your server, you need to think about the directions that your application might be going, even if you don't plan on implementing any of these features for a while. Keep in mind that there may be thousands of users hitting your server for years to come, and they may be running version 1.0 of your software.

For example, you might want to include support for paid application upgrades. If you might want to charge a certain amount for version 2.0 or 3.0 of your program, you might want to provide upgrade price data, even if it won't be needed for a while.

Consider the possibility that your server may temporarily be unavailable to your users. What are the ramifications of that? You might want to provide a "backup server" URL in your application to try if the primary server doesn't work. Even if your primary server were "smart," you could have a backup server be just a static web page indicating system status, so your user would automatically get the latest news about your server if they were to connect and your primary server was unavailable.

Another consideration is your servers changing URLs. Imagine that you expand so much that you need to move your server, currently on your home IP address, to a different domain or subdomain. If you provided for a "forwarding address" in your server response, you could easily move people to your new domain. You should consider setting up a different subdomain to handle your server-side requests (even if it's just the same as your web server domain) so you could split it into a different machine in the future.

Finally, be sure to think about version compatibility between any plug-in or document files you may serve to your users. If you provide plug-ins, but your API changes between versions of your applications, you want to make sure that your users don't download plug-ins that won't work with your current application. If you might have a paid upgrade in the future, you want to make sure that users of your earlier versions of your program can still access their version 1.0-compatible plug-ins.

Information Protocols

Now that you have decided to include some Internet connectivity into your application, how should you go about transferring information back and forth?

Before going much further, you should consider firewall issues. Most users have no problem accessing Web sites through Port 80, so unless you have some compelling need to do otherwise, you might as well go through that port. Naturally, if you will be transmitting confidential information, you're better off using SSL.

And unless there is a need for any real-time or open-ended connectivity, HTTP is a great protocol for simple request/response transfer. Your client makes a request, possibly sending some information along as parameters, and your server responds with what is effectively a Web page. Your client could perform a POST or GET; a GET is simpler because you can build up a simple URL of the form http://www.host.com/filepath?param1=value1. (Be sure to "escape" the special characters so that your URL is formed legally!) In Cocoa, you would just use NSURLHandle to fetch the contents of that URL. (If you need to use POST, Cocoa developers will need to dip down into Core Foundation or make use of CURLHandle (MacTech, Feb. 2002), available at http://curlhandle.sourceforge.net/.

The response that your server returns should be formatted as simply as possible. You could return HTML or plain text, but that would require some parsing on your server's end. You could return any XML format of your choosing, and your client would then parse the XML stream.

One trick that makes it especially easy for Cocoa applications to get at the data returned from the server is to format the file on the server as an XML property list! Even if your server is running Linux or Windows and knows nothing of this format, there's no reason why you can't place a "plist" file on your web server and have that streamed to the client. You could maintain and edit this file on your Mac, to make sure it's a readable format, and then upload it to your server when it changes. (See Listing 1 for an hypothetical example file. Note that we are able to place arbitrary HTML within our XML by surrounding it with the <![CDATA[ ... ]]> directives.)

Listing 1: popcorn_hello.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST
1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CurrentAppVersionNumber</key>
<string>1.0.7</string>
<key>CurrentAppFeatures</key>
<string>This update adds sound effects, and fixes a crash when searching in 
Japanese.</string>
<!-- Below is only shown to unregistered users -->
<key>Unregistered News</key>
<string><![CDATA[
<font face="Lucida Grande">Thanks for trying out Popcorn!
<p>
Be sure to try out the new <b>Garlic Butter</b> module from
the <b>Popcorn > Install More Plug-ins...</b> menu.
</font>
]]></string>
<!-- Below is only shown to REGISTERED users -->
<key>Registered News</key>
<string><![CDATA[
<font face="Lucida Grande">As a thank-you for our
registered users, we are offering free downloads of
<a href="http://www.karelia.com/peanutbutter/">PeanutButter
1.0</a> for a  limited time.  Give it a try!
</font>
]]></string>
</dict>
</plist>

The client application can convert the data stream it gets from the server into a Cocoa object (generally an NSDictionary or NSArray) by adding this category method to NSData, shown in Listing 2. (Constructing the @interface section is an exercise left to the reader.)

Listing 2: NSData+plist.m

propertyListFromXML
Create a property list given XML data retrieved from a server.
@implementation NSData( plist )
// Return a property list from the data.  Functions similarly to
// [NSDictionary dictionaryWithContentsOfFile:path]
- (id)propertyListFromXML
{
   CFPropertyListRef pList;
   CFStringRef errorString = nil;
   pList = CFPropertyListCreateFromXMLData(
      NULL,
      (CFDataRef)self,
      kCFPropertyListImmutable,
      &errorString);
   if(errorString)
   {
      NSLog(@"Error loading from Property List: %@",
         (NSString*)errorString);
      CFRelease(errorString);
   }
   return [(id)pList autorelease];
}

Conclusion

That about wraps up our little discussion on Internet connectivity. There are many other avenues that could be explored by innovative developers, but with just a little bit of work, it's possible to keep your users up-to-date.


Dan Wood once took an introductory Arabic class, but nobody in the room knew what language they were being taught. He likes to buy fruits and vegetables from the farmer's market on Tuesday mornings. He missed the last two days of WWDC this year due to the birth of his son. He is the author of Watson, an application written in Cocoa. Dan thanks Chuck Pisula at Apple for his technical help with this series, and acknowledges online code fragments from John C. Randolph, Stephane Sudre, Ondra Cada, Vince DeMarco, Harry Emmanuel, and others. You can reach him at dwood@karelia.com.

 
AAPL
$103.30
Apple Inc.
+0.80
MSFT
$45.09
Microsoft Corpora
-0.34
GOOG
$577.33
Google Inc.
+5.73

MacTech Search:
Community Search:

Software Updates via MacUpdate

TextSoap 7.4.0 - Flexible text editing u...
TextSoap is for people who work with text. TextSoap effortlessly cleans up text from endlessly different formats. Wash away unwanted characters, spaces, tabs. Fix paragraphs with hard returns at the... Read more
NetShade 6.0.2 - Browse privately using...
NetShade is an Internet security tool that conceals your IP address on the web. NetShade routes your Web connection through either a public anonymous proxy server, or one of NetShade's own dedicated... Read more
Mac DVDRipper Pro 5.0 - Copy, backup, an...
Mac DVDRipper Pro is the DVD backup solution that lets you protect your DVDs from scratches, save your batteries by reading your movies from your hard disk, manage your collection with just a few... Read more
pwSafe 3.1 - Secure password management...
pwSafe provides simple and secure password management across devices and computers. pwSafe uses iCloud to keep your password databases backed-up and synced between Macs and iOS devices. It is... Read more
StatsBar 1.8 - Monitor system processes...
StatsBar gives you a comprehensive and detailed analysis of the following areas of your Mac: CPU usage Memory usage Disk usage Network and bandwidth usage Battery power and health (MacBooks only)... Read more
Path Finder 6.5.5 - Powerful, award-winn...
Path Finder is a file browser that combines the familiar Finder interface with the powerful utilities and innovative features. Just a small selection of the Path Finder 6 feature set: Dual pane... Read more
QuarkXPress 10.2.1 - Desktop publishing...
With QuarkXPress, you can communicate in all the ways you need to -- and always look professional -- in print and digital media, all in a single tool. Features include: Easy to Use -- QuarkXPress is... Read more
Skype 6.19.0.450 - Voice-over-internet p...
Skype allows you to talk to friends, family and co-workers across the Internet without the inconvenience of long distance telephone charges. Using peer-to-peer data transmission technology, Skype... Read more
Capo 3.1.2 - Slow down and learn to play...
Capo lets you slow down your favorite songs so you can hear the notes and learn how they are played. With Capo, you can quickly tab out your songs atop a highly-detailed OpenCL-powered spectrogram... Read more
VueScan 9.4.41 - Scanner software with a...
VueScan is a scanning program that works with most high-quality flatbed and film scanners to produce scans that have excellent color fidelity and color balance. VueScan is easy to use, and has... Read more

Latest Forum Discussions

See All

Modern Combat 5 Gets a Major Multiplayer...
Modern Combat 5 Gets a Major Multiplayer Update Posted by Jessica Fisher on September 2nd, 2014 [ permalink ] Universal App - Designed for iPhone and iPad | Read more »
Alien Creeps TD Review
Alien Creeps TD Review By Jennifer Allen on September 2nd, 2014 Our Rating: :: EXPENSIVE DEFENSESUniversal App - Designed for iPhone and iPad Alien Creeps TD would be a fun if unremarkable Tower Defense game, but its heavy focus on... | Read more »
The Journey Down: Chapter Two Review
The Journey Down: Chapter Two Review By Jennifer Allen on September 2nd, 2014 Our Rating: :: DARK YET ENTICINGUniversal App - Designed for iPhone and iPad It’s a little dark, in every sense of the word, but The Journey Down:... | Read more »
Function Space, a Social Network App for...
Function Space, a Social Network App for Science, Launches on iOS Posted by Ellis Spice on September 2nd, 2014 [ permalink ] | Read more »
Stupidfast – How Taylor Martinez Switche...
How do you make an Endless Running game more than just another Endless Running game? By adding real life prizes to it, of course! That’s the thinking behind StupidFast: a game designed for football enthusiasts, and the brainchild of former college... | Read more »
Little Raiders: Robin’s Revenge Review
Little Raiders: Robin’s Revenge Review By Jennifer Allen on September 2nd, 2014 Our Rating: :: CASUAL RAIDINGUniversal App - Designed for iPhone and iPad Combining simple combat with village building is a potent combination for... | Read more »
Treasure Tombs: Ra Deal Coming from Bulk...
Treasure Tombs: Ra Deal Coming from Bulkypix and Dark Tonic This Fall Posted by Jessica Fisher on September 2nd, 2014 [ permalink ] Dark Tonic and | Read more »
Pirate Bash Review
Pirate Bash Review By Nadia Oxford on September 2nd, 2014 Our Rating: :: BAD PIRATES, GOOD TIMESUniversal App - Designed for iPhone and iPad Pirate Bash’s turn-based battles add an intriguing twist to a typical physics game.   | Read more »
Tiny Tower Vegas Review
Tiny Tower Vegas Review By Jennifer Allen on September 2nd, 2014 Our Rating: :: STEADY DEVELOPMENTUniversal App - Designed for iPhone and iPad Build a huge tower again but Vegas-style in Tiny Tower Vegas.   | Read more »
The Manhattan Project Review
The Manhattan Project Review By Andrew Fisher on September 2nd, 2014 Our Rating: :: ROCKET SCIENCEUniversal App - Designed for iPhone and iPad The Manhattan Project offers a great Euro-style gameplay experience, but it is totally... | Read more »

Price Scanner via MacPrices.net

Apple Cuts iPad mini Prices; Non-apple Vendor...
Digitimes’ Max Wang and Joseph Tsai report that Apple is cooperating with US-based retailers including Best Buy and Amazon to cut iPad mini prices, and that channel retailers believe other brand... Read more
Apple refurbished iPads available for up to $...
Apple is offering Certified Refurbished iPad Airs for up to $140 off MSRP. Apple’s one-year warranty is included with each model, and shipping is free. Stock tends to come and go with some of these... Read more
Are We Now In The Post-Post-PC Era?
A longtime and thoroughgoing laptop aficionado, I was more than a little dismayed by Steve Jobs’s declaration back in 2010 when he sprang the iPad on an unsuspecting world. that we’d entered a “post-... Read more
PC Outlook Improves, But 2014 Shipments Still...
According to the International Data Corporation (IDC) Worldwide Quarterly PC Tracker, worldwide PC shipments are expected to fall by -3.7 percent in 2014. To hat’s actually an improvement from the... Read more
IDC Lowers Tablet Sales Projections for 2014...
Following a second consecutive quarter of softer than expected demand, International Data Corporation (IDC) has lowered its worldwide tablet plus 2-in-1 forecast for 2014 to 233.1 million units. The... Read more
Apple now offering refurbished 21-inch 1.4GHz...
The Apple Store is now offering Apple Certified Refurbished 21″ 1.4GHz iMacs for $929 including free shipping plus Apple’s standard one-year warranty. Their price is $170 off the cost of new models,... Read more
Save $50 on the 2.5GHz Mac mini, on sale for...
B&H Photo has the 2.5GHz Mac mini on sale for $549.99 including free shipping. That’s $50 off MSRP, and B&H will also include a free copy of Parallels Desktop software. NY sales tax only. Read more
Save up to $300 on an iMac with Apple refurbi...
The Apple Store has Apple Certified Refurbished iMacs available for up to $300 off the cost of new models. Apple’s one-year warranty is standard, and shipping is free. These are the best prices on... Read more
The Rise of Phablets
Carlisle & Gallagher Consulting Group, a businesses and technology consulting firm focused solely on the financial services industry, has released an infographic depicting the convergence of... Read more
Bad Driver Database App Allows Good Drivers t...
Bad Driver Database 1.4 by Facile Group is a new iOS and Android app that lets users instantly input and see how many times a careless, reckless or just plain stupid driver has been added to the... Read more

Jobs Board

*Apple* Retail - Multiple Positions (US) - A...
Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, you're also the Read more
*Apple* Retail - Multiple Positions (US) - A...
Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, you're also the Read more
*Apple* Retail - Multiple Positions (US) - A...
Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, you're also the Read more
*Apple* Retail - Multiple Positions (US) - A...
Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, you're also the Read more
Senior Event Manager, *Apple* Retail Market...
…This senior level position is responsible for leading and imagining the Apple Retail Team's global event strategy. Delivering an overarching brand story; in-store, Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.