0

Apple offers a method within the StoreKit framework to detect whether or not the users has an apple music subscription, although, it simply does not work. I have an Apple Music subscription, and can both listen to songs from within my app and can add items to my library from within my app--yet, when I request my capabilities, it does not say I have either. Here is my code:

let serviceController = SKCloudServiceController()
serviceController.requestCapabilities { (capability:SKCloudServiceCapability, err:Error?) in
    print("error in capability check is \(err)")
    switch capability {
    case SKCloudServiceCapability.musicCatalogPlayback:
        print("user has Apple Music subscription and can play music from apple music api")
    case SKCloudServiceCapability.addToCloudMusicLibrary:
        print("user has an Apple Music subscription, can play music from api, also can add to their music library")
    case SKCloudServiceCapability.musicCatalogSubscriptionEligible:
        print("user does not have subscription")
    default:
        print("default and capability is \(capability.rawValue)")
    }
}

Every time I run my app, it prints "default and capability is 257." I've tried uninstalling/reinstalling probably a dozen times--the same every time.

Note, I can indeed get my user token and add songs to my library via the Apple Music api.

Anyone else experienced this, and is there another workaround to see whether the user has an Apple Music subscription?

jjjjjjjj
  • 4,203
  • 11
  • 53
  • 72

2 Answers2

1

capability is an OptionSet as the user may have musicCatalogPlayback or addToCloudMusicLibrary or both capabilities.

The raw value you are seeing is the bitwise OR of the actual values, since, as you say, you have both capabilities.

You can use a series of if statements or a specific if statement to check for the capability you need:

let serviceController = SKCloudServiceController()
serviceController.requestCapabilities { (capability:SKCloudServiceCapability, err:Error?) in
    guard err == nil else {
        print("error in capability check is \(err!)")
        return
    }
            
    if capability.contains(SKCloudServiceCapability.musicCatalogPlayback) {
        print("user has Apple Music subscription and can play music from apple music api")
    }

    if capability.contains(SKCloudServiceCapability.addToCloudMusicLibrary) {
        print("user has an Apple Music subscription, can play music from api, also can add to their music library")
    }

    if capability.contains(SKCloudServiceCapability.musicCatalogSubscriptionEligible) {
        print("user does not have subscription")
    }
}

gives

user has Apple Music subscription and can play music from apple music api

user has an Apple Music subscription, can play music from api, also can add to their music library

Community
  • 1
  • 1
Paulw11
  • 108,386
  • 14
  • 159
  • 186
0

You can use below function to check the Apple Music subscription not working / user does not have subscription.

var cloudServiceController: SKCloudServiceController? var cloudServiceSetUpController: SKCloudServiceSetupViewController?

internal func appleMusicRequestCapabilities(){

    if let aController = AppleMusicSharedInstance.shared.cloudServiceController{

        aController.requestCapabilities { (capabilities, error) in

            if error != nil{
                debugPrint("Error", error ?? "Unexpected Error")
            }else{
                if capabilities.contains(SKCloudServiceCapability.musicCatalogPlayback){
                    debugPrint("musicCatalogPlayback")
                }
                if capabilities.contains(SKCloudServiceCapability.musicCatalogSubscriptionEligible){
                    debugPrint("musicCatalogSubscriptionEligible")
                    DispatchQueue.main.async {
                        self.setUpSubscriptionView()
                    }
                }
                if capabilities.contains(SKCloudServiceCapability.addToCloudMusicLibrary){
                    debugPrint("addToCloudMusicLibrary")
                }
            }
        }
    }
}

Below method can be used to display the music subscription view where user can choose the subscription.

internal func setUpSubscriptionView(){

    if let aController = AppleMusicSharedInstance.shared.cloudServiceSetUpController{
        aController.load(options: [SKCloudServiceSetupOptionsKey.action: SKCloudServiceSetupAction.subscribe], completionHandler: { (success, error) in
            DispatchQueue.main.async {
                if success{
                    AppDelegate.delegate().window?.rootViewController?.present(aController, animated: true, completion: nil)
                }
                else if (error != nil){
                    debugPrint("Error", error ?? "Unexpected Error")
                }
            }
        })
    }
}
CrazyPro007
  • 1,006
  • 9
  • 15