Instance Property

location

The location of this media item if available.

Declaration

var location: URL? { get }

Discussion

If the location of this media item is not available, this property returns ITLibMediaItemLocationTypeUnknown.

This method returns URLs that are outside of the default sandbox. In order to use the iTunesLibrary framework, a sandboxed application 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 the startAccessingSecurityScopedResource function.

Listing 1

Accessing a media file from a sandboxed application

#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 non-sandboxed applications, you can use the returned location URL to access the media item file directly.