10

I've been looking at adding iTunes Library access to a Mac app I'm developing.

I looked at some of the existing open source implementations, such as EyeTunes and iMedia - but found them to not be quite what I was looking for. Overkill in general.

So I tried out using the Scripting Bridge, and found that while functional it was slow. And since I didn't really need to control iTunes, the slowness was not worth it. It also had the downside of relying on iTunes to be running. It also is a bit irritating to get it working in a sandboxed app (though possible.)

So I finally started to roll my own by reading the iTunes Library.xml file. That seems to be working OK. But in the process of investigating the meaning of the Distinguished Kind key/value I stumbled upon a brand new, Apple supplied iTunesLibrary.framework !!!

Beautiful - the iTunesLibrary framework docs are here.

However, when I try to initialize the framework using the code as shown in the reference link:

NSError * error = nil;
ITLibrary* library = [ITLibrary libraryWithAPIVersion:@"1.0" error:&error];

It returns nil and provides the very helpful error like so (in my Sandboxed app):

Error Domain=NSPOSIXErrorDomain Code=100001 "Could not load." UserInfo=0x1140025b0 {NSLocalizedDescription=Could not load., NSUnderlyingError=0x1026fbe20 "The operation couldn’t be completed. (OSStatus error -50.)"}

Thinking it might be a sandboxing issue, I also created a quick command line utility that attempts to init the library and it returns an almost identical error:

Failed to open library: Error Domain=NSPOSIXErrorDomain Code=100001 "Could not load." UserInfo=0x1018015b0 {NSLocalizedDescription=Could not load., NSUnderlyingError=0x103a000e0 "The operation couldn’t be completed. (OSStatus error 100005.)"}

Has anyone else tried out this new iTunes Library framework with anymore luck than I have had?

// EDIT: Answer below is correct. Code signing is required. The sample code in the answer is a handy starting point for using the framework. Additionally, in my case the problem was also that the iTunesLibrary framework code does not appear to work when the iTunes library is hosted on a network drive. Make this a bit of a deal breaker unless Apple fixes it.

Community
  • 1
  • 1
sckor
  • 457
  • 4
  • 13
  • I am having the exact same error with the un-sandboxed code, but don't know how to fix it. I also had some trouble importing the Framework. I finally got it working by copy and pasting the framework into my project directory and adding that to the project. I'm wondering how it knows where to look for the iTunes Library. – abroekhof May 18 '13 at 14:37
  • I was going to try this out to learn some objective-c but I can't even add it correctly to my project. Guessing the same issues as abroekhof... It adds to the project but I can´t import the header (not found). I have seen others using the scripting bridge. That may be the way to go? How did you add it to your project? – Tobias Ritzau May 19 '13 at 16:53
  • abroekhof - You were able to get the library to initialize successfully in a code signed app by copying the Framework directly into your project? – sckor May 19 '13 at 20:05
  • @sckor yes, is this bad? I actually just upgraded to Mountain Lion and now my app doesn't work anymore. What a drag. I'm getting the Could not load error as well. – abroekhof May 30 '13 at 21:16
  • I did file a bug report with Apple. To which they almost immediately responded they could not reproduce. I provided them more details about my setup and haven't heard anything back since then. So I just went on with my original plan of accessing the iTunes Library.xml directly and forgetting about the new framework for now. Sorry to hear you're having trouble with the Framework now. :( I'd suggest filing a bug report so they know there are problems with the framework that still need to be worked on. – sckor May 31 '13 at 12:48

3 Answers3

7

You'll need to codesign your app using a certificate from the Mac Developer Program.

It's not documented anywhere, but, my suspicion is that this requirement was added due to iTunes cloud being a web service thing that requires accessing user accounts.

// Edit: In addition to setting CODE_SIGN_IDENTITY in the project settings as discussed above, you will also need to add /Library/Frameworks to the FRAMEWORK_SEARCH_PATHS in the project settings so that Xcode will know where to look for the #import's.

// Edit 2: I wrote a quick little sample app to show how iTunesLibrary is used, since, Apple's sample code and documentation is rather sparse: https://github.com/zadr/iTunesLibraryExample

zadr
  • 2,505
  • 18
  • 19
  • I've tried it both ways - code signed, as well as not and it doesn't work in either case. Did you actually get it the framework to initialize? – sckor May 19 '13 at 20:03
  • Yes. I can access an ITLibrary created from the framework without any problem once `CODE_SIGN_IDENTITY = Mac Developer` is set in the xcodeproj's build settings. – zadr May 19 '13 at 20:09
  • Thanks for taking the time to pull together the sample project and post it to Github! I'm still having the same problem - code signing is setup, and it's using the default Xcode created Mac Developer team profile... Since it does appear to be code signing related somehow, I'll try fooling some more with the profiles and see if I can get it to the next step. – sckor May 20 '13 at 00:51
  • Boy this is frustrating - I took your sample code, used Xcode's archive feature. Created a distribution version signed with my Developer ID from the Organizer and it still fails: 2013-05-19 9:34:10.389 PM iTunesLibraryExample[53897]: error: Error Domain=NSPOSIXErrorDomain Code=100001 "Could not load." UserInfo=0x7f9faac0d840 {NSLocalizedDescription=Could not load., NSUnderlyingError=0x7f9faac495c0 "The operation couldn’t be completed. (OSStatus error -50.)"} – sckor May 20 '13 at 01:34
  • So it turns out that there is a bug in the iTunesLibrary framework - in my opinion. My default iTunes library is on a network drive. I relaunched iTunes and pointed it to one in my local Music directory and now the framework works. I will file a bug report for this; sort of a silly assumption on their part. – sckor May 20 '13 at 02:03
  • @zadr I can't get your code to work either since I upgraded to 10.8. It does not seem to be adding the iTunesLibrary headers from the framework. It then has the build error 'iTunesLibrary/ITLibrary.h' file not found. – abroekhof May 30 '13 at 21:41
  • 1
    @abroekhof Update your version of iTunes. And old version of iTunes shipped with a broken framework. – zadr May 30 '13 at 21:58
  • @zadr Really not sure--I have the latest iTunes. I've done Software Update inside and outside iTunes, restarted, re-added the framework. Not sure what else it could be. This is what it looks like in Xcode: http://imgur.com/MjlXvRd . Thanks for your help with this, by the way. – abroekhof May 30 '13 at 22:20
  • 1
    Re-download from itunes.com. – zadr May 30 '13 at 22:55
  • 5
    It seems to me that not only signing, general sandboxing, and adding the framework to the search part is essential, you also need to explicitly allow access to music and movies via `com.apple.security.assets.movies.read-only` and `com.apple.security.assets.music.read-only` in the entitlements. Like [so](https://github.com/zadr/iTunesLibraryExample/blob/master/iTunesLibraryExample.entitlements). – Hendrik Oct 02 '14 at 16:45
4

From iTunes Library Framework Reference

  • You must code sign your app in order to get information back from the iTunes Library framework.
  • The iTunes Library framework is available to users running iTunes v11.0 or above.
Lucas Derraugh
  • 6,929
  • 3
  • 27
  • 43
Parag Bafna
  • 22,812
  • 8
  • 71
  • 144
  • You are correct. However, the problem was a bug in the iTunes Library Framework. I opened a bug report with Apple, and it has been fixed in the latest version of iTunes. – sckor Nov 01 '13 at 11:38
1

Not sure if this is true for all of you, but in my case, I kept Code-Signing-Identity to - helped me. Now I don't need to sign the app.

Sunil Chauhan
  • 2,074
  • 1
  • 15
  • 33