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

< Previous PageNext Page > Hide TOC

Downloading Enclosures

Some entries may contain links to files as enclosures. Publication Subscription provides features that make it easy to download the files within the enclosure. Each enclosure is stored as a enclosure object in its associated entry object. Because there may be more than one enclosure in an entry, the entry object property enclosures returns an array of enclosure objects. In most cases, this array only contains one object. Each of these objects contains information about the enclosure’s size, URL and MIME type.

By default, enclosures are not automatically downloaded. You can change this setting on a per-feed basis so that any enclosure from a subscribed feed is downloaded with the entry. Listing 4-2 shows how to make a feed download its enclosures automatically.

Listing 4-2  Downloading enclosures automatically

PSFeedSettings *settings = feed.settings;
settings.downloadsEnclosures = YES;
feed.settings = settings;

If you want to download the file in the enclosures individually, send download: to the appropriate enclosure object. The download: method is asynchronous, so it rarely returns an error. Instead, check on the status of the download with the downloadState property. There are six possible states:

If the download failed, see what caused the failure by using the downloadError method. If the download is still active, you can check on its progress by using the downloadProgress method. Assuming the download finishes, the location of the downloaded file is available with the downloadedPath property.

Although the download status can be checked in a synchronous manner, it is recommended that you register for the notification PSEnclosureDownloadStateDidChangeNotification instead. When your callback method is invoked, you can determine the status of the download. Listing 4-3 shows how to start downloading the file in the enclosure and register for the appropriate notification. Listing 4-4 shows a callback method for the notification.

Listing 4-3  Downloading an enclosure

 
// Get the enclosures from the current entry, and retrieve the first one
NSArray *enclosureArray = entry.enclosures;
enclosure = [enclosureArray objectAtIndex: 0];
NSError *error;
 
// Download the enclosure
if (![enclosure download:&error]) {
    NSLog(@"Enclosure download failed: %@", error)
} else {
 
    // Register for any changes to the download's state
    [[NSNotificationCenter defaultCenter]
            addObserver:self
            selector:@selector(downloadStateChanged:)
            name: PSEnclosureDownloadStateDidChangeNotification
            object: enclosure];
}

Listing 4-4  Receiving download state changes through notifications

 
- (void) downloadStateChanged: (NSNotification *) sender {
 
    // See what state change cause the notification to be sent
    switch (enclosure.downloadState) {
 
        // If the download failed, log why and stop receiving notifications
        case PSEnclosureDownloadStateDidFail:
            NSLog(@"Enclosure download failed: %@", enclosure.downloadError);
            [[NSNotificationCenter defaultCenter]
                removeObserver: self
                name: PSEnclosureDownloadStateDidChangeNotification
                object: enclosure];
            break;
 
        // If the download succeeded, log the location of the file and stop
        // receiving notifications
        case PSEnclosureDownloadStateDidFinish:
            NSLog(@"Location of downloaded file is: %@", enclosure.downloadedPath);
            [[NSNotificationCenter defaultCenter]
                removeObserver: self
                name: PSEnclosureDownloadStateDidChangeNotification
                object: enclosure];
            break;
 
        case default:
            break;
    }
}


< Previous PageNext Page > Hide TOC


Last updated: 2007-05-11




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