DJ-Glock
DJ-Glock

Reputation: 1411

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?

Upvotes: 3

Views: 772

Answers (2)

路贵斌
路贵斌

Reputation: 1

it's bit arithmetic

.musicCatalogPlayback(1 << 0 = 1) | . addToCloudMusicLibrary(1 << 8 = 256) = 257

use

swift:

case SKCloudServiceCapability.addToCloudMusicLibrary|SKCloudServiceCapability.musicCatalogPlayback:{
    //code
}break;

oc:

case SKCloudServiceCapabilityAddToCloudMusicLibrary|SKCloudServiceCapabilityMusicCatalogPlayback:{
            //code
        }break;

Upvotes: 0

DJ-Glock
DJ-Glock

Reputation: 1411

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?")  

        }  
    }  
}  

Upvotes: 7

Related Questions