-->

Problem programmatically accessing the iTunes libr

2019-09-12 06:50发布

问题:

I built an app that must access the iTunes library. I used MPMediaQuer to retrieve songs from the library as in the following.

MPMediaQuery *media = [[MPMediaQuery alloc]init];
NSArray *arr = [media items];

for(MPMediaItem *song in arr)
{
    //To print songs title
    NSString *title = [song valueForProperty:MPMediaItemPropertyTitle];
    NSLog(@"%@", title);
}

But nothing printed!

And I tried to print the count of the arr array and the result is 0 as in the following.

NSLog(@"%D",[arr count]);

And I'm sure there are some songs in the iTunes Library.

Do this problem occur because I test in the simulator? And if I test the code on device should it work?

回答1:

The iPod Library Access APIs only works on devices, that's why you aren't getting any result on the iPhone Simulator:

Note: iPod library access works only on devices and not in the Simulator. This is because the Simulator has no access to a device’s iPod library. To develop applications using the classes in this technology, you need a provisioned iOS device.