Skip to content

Instantly share code, notes, and snippets.

@gboyegadada
Created January 11, 2026 07:44
Show Gist options
  • Select an option

  • Save gboyegadada/97d4eda75797fdfc2f30c9af0abd513d to your computer and use it in GitHub Desktop.

Select an option

Save gboyegadada/97d4eda75797fdfc2f30c9af0abd513d to your computer and use it in GitHub Desktop.
[macOs] iTunes Music Media Library Access using Swift Code Example
//
// 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