MPMediaLibrary addItemWithProductID

I'm looking into the new APIs in MPMediaLibrary in iOS 9.3 and running into a few issues.


I am using SKCloudServiceController to request authorization and then check for the AddToCloudMusicLibrary capability. I then request authorization to the MPMediaLibrary (which appears not to do anything as the SKCloudServiceController auth already does that as far as I can tell...) and then try and do the following:


MPMediaLibrary.defaultMediaLibrary().addItemWithProductID(self.trackID, completionHandler: { (entities, error) in

})


This is giving me the following error:


Error Domain=MPErrorDomain Code=2 "The requested operation is not enabled for this device." UserInfo={NSLocalizedDescription=The requested operation is not enabled for this device.}


Initially I thought it may be a problem with my product ID although this doesn't seem to be the case as I can play the track just fine using the new setQueueWithStoreIDs method on MPMusicPlayerController.


There is very little in the way of documentation for these new APIs but I'm doing everything correctly as far as I can see. Can anyone shed some light on how I can add Apple Music tracks to the user's iCloud library?

Accepted Reply

In the end I managed to get this working on a different device with the exact same code. It works on an iPad Pro but not on an iPhone 5c - I suspect it may be tied to the 64-bit processor.

Replies

Use iTunes' search API

I finally found the bug.


The iTunes Search API returns the productID as a NSNumber. But the method [MPMediaPlaylist addItemWithProductID:] only works with NSString. For some reason this is not true when trying to play a song, which makes it really hard to find the cause of the bug.


To fix it, simply call stringValue on the returned object from iTunes API.



Abras

I'm confused by your line 41...


track *t1 = [[track alloc]init];


I'm am decent at Swift and just learning Objective-C. I can't for the life of me figure out what you are doing with that code..where is "track" coming from and what is t1 supposed to represent?

I know it exactly where was the problem. Please remeber the country. Call request with country code, for example:


"https://itunes.apple.com/search?term=\(searchTerm)&entity=song&limit=50&country=SK"


If you do not use the country code, some music in store is not accessible for your country and you can get this error: Error Domain=MPErrorDomain Code=0 "An unknown error occurred." .

i have the same problem here, did you solved it?