Apple Developer Connection
Member Login Log In | Not a Member? Contact ADC

< Previous PageNext Page > Hide TOC

Legacy Documentclose button

Important: URL Access Manager is deprecated as of Mac OS X v10.4. You should use CFNetwork instead (as described in CFNetwork Programming Guide).

Case Study: Downloading Data From Multiple URLs

This section describes how the sample application Downloader downloads data from multiple URLs and stores it in multiple files using URL Access Manager function URLDownload. Downloader obtains the URLs to be downloaded by reading a text file in which they have been stored.

“Listing 1-11” illustrates how Downloader’s main function sets up the main event loop and calls the function getURL to obtain a URL from a file of URLs.

Listing 1-11  The Downloader application’s main function

#include <Events.h>
#include <stdio.h>
#include "URLAccess.h"
#include "string.h"
#include "Memory.h"
 
void main (void) {
    OSStatus err = noErr;
    char url[255];
    int count, fileCount = 0;
    EventRecord ev;
 
    // Call MaxApplZone, MoreMasters.
    // Initialize graphics port, fonts, menus, cursor, and dialogs.
    // Clear the screen.
 
    while ( url != NULL )
    {
        // Handle Events through each loop
        WaitNextEvent(everyEvent, &ev, 0, NULL);
        eventHandler( NULL, &ev );
 
        // Obtain a URL from the file of URLs
        result = getURL (url); // getURL function not shown
        if (result == eofErr) { // Handle error condition. }
 
        // Call Download function.
        result = DoDownload (url);
        if (result != noErr) { // Handle error condition. }
    }
    printf("\n All of the URLs have been downloaded.\n");
}

The DoDownload function shown in “Listing 1-12” does the actual work of downloading data from the URL. It creates a file specification for the data that is to be downloaded and a URL reference. It specifies the mask kURLReplaceExistingFlag in the openFlags parameter to replace an existing file (if any) with the downloaded data and to display a progress indicator during the download. Finally, it calls the function URLDownload to download the data.

Listing 1-12  Downloader’s DoDownload function

void DoDownload (void) {
    URLReference urlRef;
    FSSpec dest, *destPtr = NULL;
    destPtr = &dest;
    Handle destHandle = NULL;
    int openFlags = kURLReplaceExistingFlag + kURLDisplayProgressFlag;
    Str255 newFile;
 
    // Create the file specification for the download.
    sprintf((char*)newFile, "File %d", fileCount);
    c2pstr((char*)newFile);
    fileCount++;
    err = FSMakeFSSpec(0, 0, newFile, &dest);
 
    // Create the URLReference.
    err = URLNewReference( theURL, &urlRef );
    if (err != noErr) printf("URLNewReference failed\n");
 
    // Download the data.
    err = URLDownload( urlRef, destPtr, destHandle, openFlags, &eventHandler,
        (void*)&fileCount );
    if (err != noErr) printf("URLDownload failed\n");
 
    // Clean up.
    err = URLDisposeReference( urlRef );
    if (err != noErr) printf("URLDisposeReference failed\n");
    return err;
}

“Listing 1-13” illustrates Downloader’s general event handling function eventHandler. This function handles system events that might occur during calls to the functions URLSimpleDownload, URLDownload, URLSimpleUpload, and URLUpload. The userContext parameter can be used to associate any particular call of URLDownload with any particular call of the system event callback function. In this context, it is an integer.

Listing 1-13  Downloader’s system event callback function

pascal long eventHandler (void * userContext, EventRecord* eventPtr)
{
    EventRecord* ev;
    int what = 0;
    int context = 0;
    int* intPtr = NULL;
 
    // Convert the event pointer into an event record.
    ev = (EventRecord*)eventPtr;
    what = ev->what;
 
    // Convert the void* to an integer.
    intPtr = (int*)userContext;
    context = *intPtr;
    if (context < 0 || context > 99) {
        context = -1; // Unknown context
    }
 
    switch (what) {
        case 0 : // Null Event
            break;
        case mouseDown:
            printf("Handler: mouseDown  User Context: %d\n", context);
            // Call function to handle event.
            break;
        case updateEvt:
            printf("Handler: updateEvt  User Context: %d\n", context);
            // Call function to handle event.
            break;
        case activateEvt:
            printf("Handler: activateEvt  User Context: %d\n", context);
            // Call function to handle event.
            break;
        case keyDown:
            printf("Handler: keyDown  User Context: %d\n", context);
            // Call function to handle event.
            break;
        default:
            printf("Handler: Default  User Context: %d\n", context);
            break;
    }
 
    return NULL;
}


< Previous PageNext Page > Hide TOC


Last updated: 2007-05-03




Did this document help you?
Yes: Tell us what works for you.

It’s good, but: Report typos, inaccuracies, and so forth.

It wasn’t helpful: Tell us what would have helped.
Get information on Apple products.
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Copyright © 2007 Apple Inc.
All rights reserved. | Terms of use | Privacy Notice