Working With the Internet

Opening Web Pages
Two new menu items added to EdenMath 1.1.1 are EdenMath Product Page and Send Feedback. Both of these features use NSWorkspace's openURL function, which will open up a web page in your default web browser. The following code for the goToProductPage function shows how to open up a web page from a Cocoa application.

    - (IBAction) goToProductPage: (id) sender
    {
        [[NSWorkspace sharedWorkspace] openURL: [NSURL URLWithString:@"http://www.edenwaith.com/products/edenmath/"]];
    }

If you want to send an e-mail instead of opening up a web page, just change the web address to an e-mail address like mailto:my_name@mydomain.com.
Check For New Updates (a.k.a. Internet Version Checking)
One handy feature is the ability to check for new product updates. EdenMath, like many of Edenwaith's products, has a Check For Updates menu item in the EdenMath menu.
The function checkForNewVersion: retrieves two basic pieces of information: the version number of the software on the local machine, and the version number of the most current update. The current version number is retrieved from NSBundle, and this value is stored in an NSString. The latest version number is retrieved from an XML file, which is generally hosted on the Web.
Once these two pieces of information are retrieved, they are then compared. If currentVersionNumber and latestVersionNumber are not equal, then it is assumed that the software on the local machine is old. If this is true, then a dialog box appears, allowing the user to jump to a website to download the latest version. Take a look through the code to get a feel for what it does.

    - (IBAction) checkForNewVersion: (id) sender
    {
        NSString *currentVersionNumber = [[[NSBundle bundleForClass:[self class]] infoDictionary] objectForKey:@"CFBundleVersion"];
        NSDictionary *productVersionDict = [NSDictionary dictionaryWithContentsOfURL: [NSURL URLWithString:@"http://www.edenwaith.com/version.xml"]];
        NSString *latestVersionNumber = [productVersionDict valueForKey:@"EdenMath"];
        int button = 0;

        if ( latestVersionNumber == nil )
        {
            NSBeep();
            NSRunAlertPanel(@"Could not check for update", @"A problem arose while attempting to check for a new version of EdenMath.  Edenwaith.com may be temporarily unavailable or your network may be down.", @"OK", nil, nil);
        }
        else if ( [latestVersionNumber isEqualTo: currentVersionNumber] )
        {
            NSRunAlertPanel(@"Software is Up-To-Date", @"You have the most recent version of EdenMath.", @"OK", nil, nil);
        }
        else
        {
            button = NSRunAlertPanel(@"New Version is Available", @"A new version of EdenMath is available.", @"OK", @"Cancel", nil);

            if (NSOKButton == button)
            {
                [[NSWorkspace sharedWorkspace] openURL: [NSURL URLWithString:@"http://www.edenwaith.com/downloads/edenmath.php"]];
            }
        }
    }

Notice how latestVersionNumber is checked to see if its value is empty (nil). This can happen if the version.xml file could not be found, or if there is no internet connection present. Without this check, the function might return incorrect information. The other two conditions are to respond appropriately whether or not the two version numbers match. If they are the same, then an information box appears telling the user that they have the most recent version of the application. Otherwise, a dialog box appears which allows the user to go download the latest software. Notice how this makes use of NSWorkspace's openURL, as discussed above.
However, none of this is possible without an XML file to contain the most up-to-date information. The XML file makes use of dictionary pairs: the key contains the application name, which is associated with the version or build number. Below is an example of the file version.xml. Additional applications can be added to one file by adding new key and string pairs.

    <?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>EdenMath</key>
    	<string>5</string>
    </dict>
    </plist>

Further information about Internet Version Checking can be found at http://cocoadevcentral.com/articles/000048.php.