Where to find Objective-c sample code to access Apple Music?

I've searched and searched but can not find objective-c sample code to access Apple Mussic from an app I am writing.


Any help would be appreciated.

Accepted Reply

WWDC 2017 session 502 (https://developer.apple.com/videos/play/wwdc2017/502/) and its sample code (https://developer.apple.com/sample-code/wwdc/2017/Interacting-with-Apple-Music-Content.zip) explain almost everything you want to do, but they're in Swift. Because the Apple Music features came out in the last few years during the Swift era, you are not going to find many Objective-C resources for Apple Music.


Regarding the error messages: I think those errors are unrelated, so you don't need to do anything about them.


If you need anything else, feel free to ask here.

Replies

Please be more specific: What exactly are you trying to do with Apple Music? Play a song, album, etc.? Search for something in the Apple Music catalog?

To start with:

1. See if device owner is a current Apple Music subscriber.

2. Access their playlists and play songs from them.

3. Search Apple Music and play songs.


From there I can add my new magic way of rating and playing them 😉 in my own database and interface.


I have been trying to decipher the API for item 1 and don't get very far, here is my attempt:


  SKCloudServiceController *controller = [SKCloudServiceController new];
  [controller requestCapabilitiesWithCompletionHandler:^(SKCloudServiceCapability capabilities, NSError * _Nullable error) {
    if (error != nil) {
      NSLog(@"Error getting SKCloudServiceController capabilities: %@", error);

    } else if (capabilities & SKCloudServiceCapabilityMusicCatalogPlayback) {
      // The user has an active subscription
      NSLog(@"YES SUBSCRIBED!!!!");
    } else {
      // The user does *not* have an active subscription
      NSLog(@"NOT SUBSCRIBED!!!!");
    }
  }];

Resulting in this:

Msic[1596:220976] [core] "Error returned from daemon: Error Domain=com.apple.accounts Code=9 "(null)""

Msic[1596:220976] SSAccountStore: Failed to fetch the backing accounts. error = Error Domain=com.apple.accounts Code=9 "(null)"

Msic[1596:221058] [core] "Error returned from daemon: Error Domain=com.apple.accounts Code=9 "(null)""

Msic[1596:221058] SSAccountStore: Failed to fetch the backing accounts. error = Error Domain=com.apple.accounts Code=9 "(null)"

Msic[1596:221058] SSAccountStore: Unable to get the local account. error = Error Domain=SSErrorDomain Code=100 "Cannot connect to iTunes Store" UserInfo={NSLocalizedDescription=Cannot connect to iTunes Store}

Msic[1596:220976] Error getting SKCloudServiceController capabilities: Error Domain=SKErrorDomain Code=6 "The requesting app does not have the necessary permissions" UserInfo={NSLocalizedDescription=The requesting app does not have the necessary permissions}

(lldb)


Thanks


PS: Sorry this reply took so long to appear but it was taking more than a day to get moderated because the target output of xcode uses dates in the format YYYY-MM-DD which forces moderation ??????


I just got rid of dates and time to get it through unmoderated 🙂🙂🙂

  • Hi Carmen, Do you think it's okay to share a sample working code ? I'd love to be able to play an Apple Music song through Objc as it would make things easier for me too. It's just sad that we cant use MusicKit in Objc.

  • Hello @Kagzei,

    You can always add a few Swift files to a larger Objective-C project, and call into those Swift files from your Objective-C codebase.

    You can learn more about this in this documentation article: Importing Swift into Objective-C.

    I would strongly advise you to use MusicKit with this technique; it will be considerably easier, since you'll be able to leverage automatic developer token generation for Apple Music API.

    I hope this helps.

    Best regards,

  • Hello @Kagzei,

    You can always add a few Swift files to a larger Objective-C project, and call into those Swift files from your Objective-C codebase.

    You can learn more about this in this documentation article: Importing Swift into Objective-C.

    I would strongly advise you to use MusicKit with this technique; it will be considerably easier, since you'll be able to leverage automatic developer token generation for Apple Music API.

    I hope this helps.

    Best regards,

Add a Comment

You need to ask the user for permission first.


First, add the NSAppleMusicUsageDescription key to your Info.plist file. This key contains the reason that you need the Apple Music permission, which is displayed to the user. If you don't include this key, your app will automatically crash.


Then, check the cloud service authorization, like this:

- (void)checkPermissions {
    SKCloudServiceAuthorizationStatus status = SKCloudServiceController.authorizationStatus;
    switch (status) {
        case SKCloudServiceAuthorizationStatusNotDetermined:{
            // Not determined: ask for permission.
            [SKCloudServiceController requestAuthorization:^(SKCloudServiceAuthorizationStatus status) {
                [self checkPermissions];
            }];
            break;
        }
        case SKCloudServiceAuthorizationStatusAuthorized:
            // Authorized: proceed.

            break;
        default:
            // Denied or restricted: do not proceed.
            break;
    }
}


After you get SKCloudServiceAuthorizationStatusAuthorized, you can then call the rest of your code.

WOW, that part worked perfectly 🙂, thanks.


I you don't mind where would I have looked to find that information out without bothering you?


Also, when I asked for objective-c sample code to access Apple Music, I envisioned code (or a sample app) that would get me past just seeing if the device is authorized, I assume the answer to my topic is that there is currently no objective-c sample code (or sample app) that would get me further than this, is that correct?


I will try to soldier on, hopefully it gets easier.

Next, I am trying to get the users storefrontID but I seem to run into the same errors, however I also seem to get the ID.


Here is my admittedly amatuerish code which uses yours as a starting point:


- (void)checkPermissions {
  NSString *mg;
  SKCloudServiceAuthorizationStatus status = SKCloudServiceController.authorizationStatus;
  switch (status) {
    case SKCloudServiceAuthorizationStatusNotDetermined:{
      // Not determined: ask for permission.
      [SKCloudServiceController requestAuthorization:^(SKCloudServiceAuthorizationStatus status) {
        [self checkPermissions];
      }];
      break;
    }
    case SKCloudServiceAuthorizationStatusAuthorized:
      // Authorized: proceed.
      NSLog(@"YES SUBSCRIBED!!!!");
      [self getUserStorefrontID];
      break;
    default:
      // Denied or restricted: do not proceed.
      NSLog(@"NOT SUBSCRIBED!!!!");
      mg= [NSString stringWithFormat:@"It appears that you are not currently 
      subscribed to Apple Music.  Please subscribe to Apple Music and then re-open
      this App.  This App will close when you touch OK."];
      [self msgOKend:mg];
      break;
  }
}

- (void) getUserStorefrontID{
  SKCloudServiceController *controller = [SKCloudServiceController new];
  [controller requestStorefrontIdentifierWithCompletionHandler:^(NSString * _Nullable storefrontIdentifier, NSError * _Nullable error) {
    if (error != nil) {
      userStoreFrontID= nil;
      NSLog(@"ERROR-no storefrontID");
    } else {
      userStoreFrontID= storefrontIdentifier;
      NSLog(@"Got storefrontID = %@ ", userStoreFrontID);
    }
  }];
}

And here is the time stripped output:

YES SUBSCRIBED!!!!

"Error returned from daemon: Error Domain=com.apple.accounts Code=9 "(null)""

SSAccountStore: Failed to fetch the backing accounts. error = Error Domain=com.apple.accounts Code=9 "(null)"

[core] "Error returned from daemon: Error Domain=com.apple.accounts Code=9 "(null)""

SSAccountStore: Failed to fetch the backing accounts. error = Error Domain=com.apple.accounts Code=9 "(null)"

SSAccountStore: Unable to get the local account. error = Error Domain=SSErrorDomain Code=100 "Cannot connect to iTunes Store" UserInfo={NSLocalizedDescription=Cannot connect to iTunes Store}

Got storefrontID = 143441-1,29


My question is, since I did get the storefrontID, do I need to worry aout the errors?


If I need to address the errors, what will fix this?


Thanks in advance...

WWDC 2017 session 502 (https://developer.apple.com/videos/play/wwdc2017/502/) and its sample code (https://developer.apple.com/sample-code/wwdc/2017/Interacting-with-Apple-Music-Content.zip) explain almost everything you want to do, but they're in Swift. Because the Apple Music features came out in the last few years during the Swift era, you are not going to find many Objective-C resources for Apple Music.


Regarding the error messages: I think those errors are unrelated, so you don't need to do anything about them.


If you need anything else, feel free to ask here.

Thanks, I had found that Swift example but I am too old to learn a new language even if Objective-c is some sort of subset of Swift or vice-versa 😟


Also, thanks confirming that it might be OK to ignore those errors.


I am now searching/playing Apple Music songs that I find and also accessing/playing a users playlist.


I should be fine from here 🙂🙂