Search code examples
iosswiftapple-music

Swift 3.1 Issue with SKCloudServiceCapability


I'm trying to call this function to check Apple Music subscription status. I have an active subscription and listen to music on my iPhone. But when I'm running test app on it, capability value is not valid.

It should be SKCloudServiceCapability.musicCatalogPlayback, SKCloudServiceCapability.addToCloudMusicLibrary, or not set. I can only get raw value = 257.

func appleMusicCheckIfDeviceCanPlayback()
{
    let serviceController = SKCloudServiceController()
    serviceController.requestCapabilities { (capability:SKCloudServiceCapability, err:Error?) in
        switch capability {
        case SKCloudServiceCapability.musicCatalogPlayback:
            print("The user has an Apple Music subscription and can playback music!")
        case SKCloudServiceCapability.addToCloudMusicLibrary:
            print("The user has an Apple Music subscription, can playback music AND can add to the Cloud Music Library")
        case []:
            print("The user doesn't have an Apple Music subscription available. Now would be a good time to prompt them to buy one?")
        default: print("Something went wrong")
        }
    }
}

screenshot


What's wrong here?


Solution

  • Finally guys from Apple Forums gave me this link to documentation and I have found the issue. https://developer.apple.com/library/content/qa/qa1929/_index.html

    I should use if capability.contains(SKCloudServiceCapability.) instead of switch for capability value. So this code works pretty fine.

    func appleMusicCheckIfDeviceCanPlayback()  
    {  
        let serviceController = SKCloudServiceController()  
        serviceController.requestCapabilities { (capability:SKCloudServiceCapability, err:Error?) in  
            if capability.contains(SKCloudServiceCapability.musicCatalogPlayback) {  
                print("The user has an Apple Music subscription and can playback music!")  
    
            } else if  capability.contains(SKCloudServiceCapability.addToCloudMusicLibrary) {  
                print("The user has an Apple Music subscription, can playback music AND can add to the Cloud Music Library")  
    
            } else {  
                print("The user doesn't have an Apple Music subscription available. Now would be a good time to prompt them to buy one?")  
    
            }  
        }  
    }