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.

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

coconutBattery 3.9.14 - Displays info ab...
With coconutBattery you're always aware of your current battery health. It shows you live information about your battery such as how often it was charged and how is the current maximum capacity in... Read more
Keynote 13.2 - Apple's presentation...
Easily create gorgeous presentations with the all-new Keynote, featuring powerful yet easy-to-use tools and dazzling effects that will make you a very hard act to follow. The Theme Chooser lets you... Read more
Apple Pages 13.2 - Apple's word pro...
Apple Pages is a powerful word processor that gives you everything you need to create documents that look beautiful. And read beautifully. It lets you work seamlessly between Mac and iOS devices, and... Read more
Numbers 13.2 - Apple's spreadsheet...
With Apple Numbers, sophisticated spreadsheets are just the start. The whole sheet is your canvas. Just add dramatic interactive charts, tables, and images that paint a revealing picture of your data... Read more
Ableton Live 11.3.11 - Record music usin...
Ableton Live lets you create and record music on your Mac. Use digital instruments, pre-recorded sounds, and sampled loops to arrange, produce, and perform your music like never before. Ableton Live... Read more
Affinity Photo 2.2.0 - Digital editing f...
Affinity Photo - redefines the boundaries for professional photo editing software for the Mac. With a meticulous focus on workflow it offers sophisticated tools for enhancing, editing and retouching... Read more
SpamSieve 3.0 - Robust spam filter for m...
SpamSieve is a robust spam filter for major email clients that uses powerful Bayesian spam filtering. SpamSieve understands what your spam looks like in order to block it all, but also learns what... Read more
WhatsApp 2.2338.12 - Desktop client for...
WhatsApp is the desktop client for WhatsApp Messenger, a cross-platform mobile messaging app which allows you to exchange messages without having to pay for SMS. WhatsApp Messenger is available for... Read more
Fantastical 3.8.2 - Create calendar even...
Fantastical is the Mac calendar you'll actually enjoy using. Creating an event with Fantastical is quick, easy, and fun: Open Fantastical with a single click or keystroke Type in your event details... Read more
iShowU Instant 1.4.14 - Full-featured sc...
iShowU Instant gives you real-time screen recording like you've never seen before! It is the fastest, most feature-filled real-time screen capture tool from shinywhitebox yet. All of the features you... Read more

Latest Forum Discussions

See All

The iPhone 15 Episode – The TouchArcade...
After a 3 week hiatus The TouchArcade Show returns with another action-packed episode! Well, maybe not so much “action-packed" as it is “packed with talk about the iPhone 15 Pro". Eli, being in a time zone 3 hours ahead of me, as well as being smart... | Read more »
TouchArcade Game of the Week: ‘DERE Veng...
Developer Appsir Games have been putting out genre-defying titles on mobile (and other platforms) for a number of years now, and this week marks the release of their magnum opus DERE Vengeance which has been many years in the making. In fact, if the... | Read more »
SwitchArcade Round-Up: Reviews Featuring...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for September 22nd, 2023. I’ve had a good night’s sleep, and though my body aches down to the last bit of sinew and meat, I’m at least thinking straight again. We’ve got a lot to look at... | Read more »
TGS 2023: Level-5 Celebrates 25 Years Wi...
Back when I first started covering the Tokyo Game Show for TouchArcade, prolific RPG producer Level-5 could always be counted on for a fairly big booth with a blend of mobile and console games on offer. At recent shows, the company’s presence has... | Read more »
TGS 2023: ‘Final Fantasy’ & ‘Dragon...
Square Enix usually has one of the bigger, more attention-grabbing booths at the Tokyo Game Show, and this year was no different in that sense. The line-ups to play pretty much anything there were among the lengthiest of the show, and there were... | Read more »
Valve Says To Not Expect a Faster Steam...
With the big 20% off discount for the Steam Deck available to celebrate Steam’s 20th anniversary, Valve had a good presence at TGS 2023 with interviews and more. | Read more »
‘Honkai Impact 3rd Part 2’ Revealed at T...
At TGS 2023, HoYoverse had a big presence with new trailers for the usual suspects, but I didn’t expect a big announcement for Honkai Impact 3rd (Free). | Read more »
‘Junkworld’ Is Out Now As This Week’s Ne...
Epic post-apocalyptic tower-defense experience Junkworld () from Ironhide Games is out now on Apple Arcade worldwide. We’ve been covering it for a while now, and even through its soft launches before, but it has returned as an Apple Arcade... | Read more »
Motorsport legends NASCAR announce an up...
NASCAR often gets a bad reputation outside of America, but there is a certain charm to it with its close side-by-side action and its focus on pure speed, but it never managed to really massively break out internationally. Now, there's a chance... | Read more »
Skullgirls Mobile Version 6.0 Update Rel...
I’ve been covering Marie’s upcoming release from Hidden Variable in Skullgirls Mobile (Free) for a while now across the announcement, gameplay | Read more »

Price Scanner via MacPrices.net

New low price: 13″ M2 MacBook Pro for $1049,...
Amazon has the Space Gray 13″ MacBook Pro with an Apple M2 CPU and 256GB of storage in stock and on sale today for $250 off MSRP. Their price is the lowest we’ve seen for this configuration from any... Read more
Apple AirPods 2 with USB-C now in stock and o...
Amazon has Apple’s 2023 AirPods Pro with USB-C now in stock and on sale for $199.99 including free shipping. Their price is $50 off MSRP, and it’s currently the lowest price available for new AirPods... Read more
New low prices: Apple’s 15″ M2 MacBook Airs w...
Amazon has 15″ MacBook Airs with M2 CPUs and 512GB of storage in stock and on sale for $1249 shipped. That’s $250 off Apple’s MSRP, and it’s the lowest price available for these M2-powered MacBook... Read more
New low price: Clearance 16″ Apple MacBook Pr...
B&H Photo has clearance 16″ M1 Max MacBook Pros, 10-core CPU/32-core GPU/1TB SSD/Space Gray or Silver, in stock today for $2399 including free 1-2 day delivery to most US addresses. Their price... Read more
Switch to Red Pocket Mobile and get a new iPh...
Red Pocket Mobile has new Apple iPhone 15 and 15 Pro models on sale for $300 off MSRP when you switch and open up a new line of service. Red Pocket Mobile is a nationwide service using all the major... Read more
Apple continues to offer a $350 discount on 2...
Apple has Studio Display models available in their Certified Refurbished store for up to $350 off MSRP. Each display comes with Apple’s one-year warranty, with new glass and a case, and ships free.... Read more
Apple’s 16-inch MacBook Pros with M2 Pro CPUs...
Amazon is offering a $250 discount on new Apple 16-inch M2 Pro MacBook Pros for a limited time. Their prices are currently the lowest available for these models from any Apple retailer: – 16″ MacBook... Read more
Closeout Sale: Apple Watch Ultra with Green A...
Adorama haș the Apple Watch Ultra with a Green Alpine Loop on clearance sale for $699 including free shipping. Their price is $100 off original MSRP, and it’s the lowest price we’ve seen for an Apple... Read more
Use this promo code at Verizon to take $150 o...
Verizon is offering a $150 discount on cellular-capable Apple Watch Series 9 and Ultra 2 models for a limited time. Use code WATCH150 at checkout to take advantage of this offer. The fine print: “Up... Read more
New low price: Apple’s 10th generation iPads...
B&H Photo has the 10th generation 64GB WiFi iPad (Blue and Silver colors) in stock and on sale for $379 for a limited time. B&H’s price is $70 off Apple’s MSRP, and it’s the lowest price... Read more

Jobs Board

Optometrist- *Apple* Valley, CA- Target Opt...
Optometrist- Apple Valley, CA- Target Optical Date: Sep 23, 2023 Brand: Target Optical Location: Apple Valley, CA, US, 92308 **Requisition ID:** 796045 At Target Read more
Senior *Apple* iOS CNO Developer (Onsite) -...
…Offense and Defense Experts (CODEX) is in need of smart, motivated and self-driven Apple iOS CNO Developers to join our team to solve real-time cyber challenges. Read more
*Apple* Systems Administrator - JAMF - Activ...
…**Public Trust/Other Required:** None **Job Family:** Systems Administration **Skills:** Apple Platforms,Computer Servers,Jamf Pro **Experience:** 3 + years of Read more
Child Care Teacher - Glenda Drive/ *Apple* V...
Child Care Teacher - Glenda Drive/ Apple ValleyTeacher Share by Email Share on LinkedIn Share on Twitter Share on Facebook Apply Read more
Machine Operator 4 - *Apple* 2nd Shift - Bon...
Machine Operator 4 - Apple 2nd ShiftApply now " Apply now + Start apply with LinkedIn + Apply Now Start + Please wait Date:Sep 22, 2023 Location: Swedesboro, NJ, US, Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.