Search code examples
iphoneiositunes

iOS: Is it possible to detect if a track exists in music library?


I'm working on an app for a band and they'd like certain features to unlock if their album or singles are in the user's music library.

Is it possible to scan the library for a specific title?(and possibly check duration as well?)


Solution

  • Here is a working search. This checks for a track that matches both the title and the artist name.

    MPMediaPropertyPredicate *titlePredicate = [MPMediaPropertyPredicate predicateWithValue:@"Sleep The Clock Around" forProperty:MPMediaItemPropertyTitle comparisonType:MPMediaPredicateComparisonEqualTo];
    
    MPMediaPropertyPredicate *artistPredicate = [MPMediaPropertyPredicate predicateWithValue:@"Belle & Sebastian" forProperty:MPMediaItemPropertyArtist comparisonType:MPMediaPredicateComparisonEqualTo];
    
    MPMediaQuery *trackSearch = [[MPMediaQuery alloc] initWithFilterPredicates:[NSSet setWithObjects:titlePredicate,artistPredicate, nil]];
    
    if(trackSearch.items.count > 0) NSLog(@"we found the track!");