-[MPMediaPlaylist addItemWithProductID:completionHandler:] returns not found error when item is not in user's library

Originator:jpmfagundes
Number:rdar://26408683 Date Originated:22-May-2016 00:07 AM
Status:Closed Resolved:15-Mar-2017 07:15 PM
Product:iOS SDK Product Version:9.3.2 (13F69)
Classification: Reproducible:Always
 
Summary:
-[MPMediaPlaylist addItemWithProductID:completionHandler:] always returns the following error:
Error Domain=MPErrorDomain Code=4 "The requested id could not be found" UserInfo={NSLocalizedDescription=The requested id could not be found}
When the given product ID is not present in the user's Apple Music library.

For this example let's use product ID 1049605633, it's the song Sorry by Justin Bieber (bear with me, it's just something I didn't have in my library haha) on the US store.

I don't have that song in my library. Using the following code...

MPMusicPlayerController *musicPlayer = [MPMusicPlayerController systemMusicPlayer];  
[musicPlayer setQueueWithStoreIDs:@[@"1049605633"]];  
[musicPlayer play];

...the song plays.

Now consider this code:

[[MPMediaLibrary defaultMediaLibrary] getPlaylistWithUUID:uuid creationMetadata:[[MPMediaPlaylistCreationMetadata alloc] initWithName:@"Test Playlist"] completionHandler:^(MPMediaPlaylist * _Nullable playlist, NSError * _Nullable error) {  
    NSLog(@"%@", error);

    if (!error) {  
        [playlist addItemWithProductID:@"1049605633" completionHandler:^(NSError * _Nullable error) {  
            NSLog(@"%@", error);  
        }];  
    }  
}];

The playlist is created, but when trying to add the product ID 1049605633 to it I get the error mentioned before.
But, if I do this before...

[[MPMediaLibrary defaultMediaLibrary] addItemWithProductID:@"1049605633" completionHandler:^(NSArray<__kindof MPMediaEntity *> * _Nonnull entities, NSError * _Nullable error) {  
    NSLog(@"%@", error);  
}];

...it does work. But that's no good, I don't want the song in the user's library.

Steps to Reproduce:
1. Get user's authorization to access Apple Music with +[SKCloudServiceController requestAuthorization:]
2. Run the following code:

[[MPMediaLibrary defaultMediaLibrary] getPlaylistWithUUID:[NSUUID UUID] creationMetadata:[[MPMediaPlaylistCreationMetadata alloc] initWithName:@"Test Playlist"] completionHandler:^(MPMediaPlaylist * _Nullable playlist, NSError * _Nullable error) {
    if (!error) {
        [playlist addItemWithProductID:@"1049605633" completionHandler:^(NSError * _Nullable error) {
            NSLog(@"%@", error);
        }];
    }
}];

3. If you don't have the song Sorry by Justin Bieber you should get this error: Error Domain=MPErrorDomain Code=4 "The requested id could not be found" UserInfo={NSLocalizedDescription=The requested id could not be found}


Expected Results:
The specified item with product ID should be added to the playlist even if the user doesn't have it in their library.

Actual Results:
If the user doesn't have that item in their library the following error is returned:
Error Domain=MPErrorDomain Code=4 "The requested id could not be found" UserInfo={NSLocalizedDescription=The requested id could not be found}


Version:
9.3.2 (13F69)

Notes:


Configuration:
iPhone 6s 64gb, US iTunes account

Attachments:

Comments

Apparently if you wait ~5 seconds before adding songs to the playlist, it works

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 5 * NSEC_PER_SEC), dispatch_get_main_queue()), ^() { [playlist addItemWithProductID:@"316654632" completionHandler:^(NSError * _Nullable error) {
NSLog(@"%@", error);
}];

ref http://stackoverflow.com/a/37479692/1757960

By jpmfagundes at May 27, 2016, 3:57 p.m. (reply...)

Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!