Contents

location

The location of the media item on disk.

Declaration

var location: URL? { get }

Discussion

If the location of the media item isn’t available, this property returns ITLibMediaItemLocationType.unknown.

This method returns URLs that are outside of the default sandbox. To use the iTunesLibrary framework, a sandboxed app must have the com.apple.security.assets.music.read-write or com.apple.security.assets.music.read-only entitlement.

To access the media item file in a sandboxed app, call startAccessingSecurityScopedResource().

Listing 1.

#import <iTunesLibrary/ITLibrary.h>
#import <iTunesLibrary/ITLibMediaItem.h>
 
// Initialize
    NSError * error = nil;
    ITLibrary* library =
        [[ITLibrary alloc] initWithAPIVersion:@"1.0" error:&error];
    if (library)
    {
        // Get first media item in the library
        NSArray * mediaItems = library.allMediaItems;
        if (mediaItems.count > 0)
        {
            // Get the location (URL) of the first media item and
            // prepare it for file read/write access.
            ITLibMediaItem * mediaItem =
                (ITLibMediaItem*)[mediaItems objectAtIndex:0];
            NSURL * mediaItemLocation = mediaItem.location;
 
            if (mediaItemLocation &&
                [mediaItemLocation startAccessingSecurityScopedResource])
            {
                // mediaItemLocation can be now used to read/write
                // the media file
                [mediaItemLocation stopAccessingSecurityScopedResource];
            }
        }
    }

For more information about using URLs that are outside the default sandbox from a sandboxed app, see Security-Scoped Bookmarks and Persistent Resource Access.

In nonsandboxed apps, you can use the returned location URL to access the media item file directly.

See Also

Getting Media Item Info