3

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?

4

2 回答 2

7

最后,来自 Apple 论坛的人给了我这个文档链接,我发现了这个问题。 https://developer.apple.com/library/content/qa/qa1929/_index.html

我应该使用 if capability.contains(SKCloudServiceCapability.) 而不是 switch 作为能力值。所以这段代码工作得很好。

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

        }  
    }  
}  
于 2017-05-15T12:59:46.077 回答
0

这有点算术

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

利用

swift:

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

oc:

case SKCloudServiceCapabilityAddToCloudMusicLibrary|SKCloudServiceCapabilityMusicCatalogPlayback:{
            //code
        }break;
于 2018-10-11T08:21:59.657 回答