Hi,
I used this code in iOS9 & iOS10 without issue, but in iOS11, the call at line 11 to addItemWithProductID just hangs. Does anyone see anything I'm doing wrong or know if there is a better way to handle adding an Apple Music track to a user's library in iOS11?
-(void) addAppleMusicTrackWithProductID: (NSString *) productID
{
MPMusicPlayerController *musicPlayer = [MPMusicPlayerController systemMusicPlayer];
[SKCloudServiceController requestAuthorization:^(SKCloudServiceAuthorizationStatus status) {
SKCloudServiceController *cloudServiceController;
cloudServiceController = [[SKCloudServiceController alloc] init];
[cloudServiceController requestCapabilitiesWithCompletionHandler:^(SKCloudServiceCapability capabilities, NSError * _Nullable error) {
if (capabilities >= SKCloudServiceCapabilityAddToCloudMusicLibrary)
{
[[MPMediaLibrary defaultMediaLibrary] addItemWithProductID:productID completionHandler:^(NSArray<__kindof MPMediaEntity *> * _Nonnull entities, NSError * _Nullable error) {
NSLog(@"%@", error);
NSArray *tracksToPlay = [NSArray arrayWithObject:productID];
[musicPlayer setQueueWithStoreIDs:tracksToPlay];
[musicPlayer play];
[self performSelectorOnMainThread:@selector(grabPlayDataFromCurrentPlayingAppleMusicTrackProductID:) withObject:productID waitUntilDone:YES];
}];
}
else
{
NSLog(@"seems the ability to add to icloud music is not there. sigh.");
}
}];
}];
}
-(void) grabPlayDataFromCurrentPlayingAppleMusicTrackProductID: (NSString *) productID
{
// musicPlayer is init & retained earlier
if ((musicPlayer.nowPlayingItem) && (musicPlayer.nowPlayingItem.playbackDuration))
{
track *t1 = [[track alloc] init]; // this track is custom class to keep track of items beyond regular track
t1.mediaitem = musicPlayer.nowPlayingItem;
t1.appleProductIDURL = productID;
t1.start = 0;
t1.crossfade = 0;
t1.end = [[musicPlayer.nowPlayingItem valueForProperty:MPMediaItemPropertyPlaybackDuration] intValue];
t1.name = [musicPlayer.nowPlayingItem valueForProperty:MPMediaItemPropertyTitle];
t1.trackArtist = [musicPlayer.nowPlayingItem valueForProperty:MPMediaItemPropertyArtist];
t1.trackAddressURL = Nil;
// add t1 to your track list or do with it whatever you need
}
else
{
NSLog(@"seems the music player doens't yet have the info so try again in 1 second");
[self performSelector:@selector(grabPlayDataFromCurrentPlayingAppleMusicTrackProductID:) withObject:productID afterDelay:1.0];
// count loops without success if necessary
}
}