Created
January 11, 2026 07:44
-
-
Save gboyegadada/97d4eda75797fdfc2f30c9af0abd513d to your computer and use it in GitHub Desktop.
[macOs] iTunes Music Media Library Access using Swift Code Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // MusicHelperExample.swift | |
| // | |
| // Created by Gboyega Dada on 11/01/2026. | |
| // | |
| // [ IMPORTANT ] | |
| // - Requires "iTunes/Media Access" | |
| // - Add plist entry: "Privacy - Music Usage Description" with a message to display | |
| // when requesting access from the user. | |
| import Foundation | |
| import iTunesLibrary | |
| class MusicHelper { | |
| var library: ITLibrary | |
| var songs: [ITLibMediaItem] = [] | |
| var playlists: [ITLibPlaylist] = [] | |
| init() throws { | |
| self.library = try ITLibrary(apiVersion: "1.1") | |
| } | |
| func fetchLibrary() { | |
| self.songs = library.allMediaItems | |
| let limit = 10 | |
| var count = 0 | |
| print("\n-- Songs --") | |
| for song in songs { | |
| print("\n\(song.title)") | |
| if let artistName = song.artist?.name { | |
| print("by \(artistName)") | |
| } | |
| guard count < limit else { break } | |
| count += 1 | |
| } | |
| self.playlists = library.allPlaylists | |
| count = 0 | |
| print("\n-- Playlists --") | |
| for playlist in playlists { | |
| print(playlist.name) | |
| guard count < limit else { break } | |
| count += 1 | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment