Using SKCloudServiceController to determine your device's music library capabilities
Q: Calling requestCapabilities(completionHandler:) always returns the None capability. How do I determine my device's current music library capabilities?
A: Calling requestCapabilities(completionHandler:) may return the None
capability for any of the following reasons:
You are not logged into Apple Music on your device.
Your app hasn't requested access to the music library on the device yet. Use SKCloudServiceController's requestAuthorization: to request access to it as demonstrated in Listing 1.
Listing 1 Asking for permission to access the music library on the device.
func requestMusicLibraryAccess()
{
SKCloudServiceController.requestAuthorization({
(status: SKCloudServiceAuthorizationStatus) in
switch(status)
{
case .Authorized: print("Access granted.")
case .Denied, .Restricted: print("Access denied or restricted.")
case .NotDetermined: print("Access cannot be determined.")
}
})
}
Your app does not have permission to access the music library on the device. Use SKCloudServiceController's authorizationStatus to determine your app's authorization status as illustrated in Listing 2.
Listing 2 Checking the app's authorization status for the music library on the device .
func checkMusicLibraryAuthorizationStatus()
{
switch SKCloudServiceController.authorizationStatus()
{
case .Authorized: print("Access granted.")
case .NotDetermined: requestAuthorization()
case .Denied, .Restricted: print("Access denied or restricted.")
}
}
You are probably not using the returned
capabilities
parameter of requestCapabilities(completionHandler:) appropriately. You should be checkingcapabilities
for the features that you are interested in.To determine whether the device supports playback of Apple Music catalog tracks, check whether
capabilities
includesSKCloudServiceCapability.MusicCatalogPlayback
as shown in Listing 3.Listing 3 Determining whether the device allows playback of Apple Music catalog tracks.
let controller = SKCloudServiceController()
controller.requestCapabilitiesWithCompletionHandler({
(capabilities: SKCloudServiceCapability, error: NSError?) in
if capabilities.contains(SKCloudServiceCapability.MusicCatalogPlayback)
{
print("The device allows playback of Apple Music catalog tracks.")
}
})
To determine whether the device allows tracks to be added to the user’s music library, check whether
capabilities
includesSKCloudServiceCapability.AddToCloudMusicLibrary
as shown in Listing 4.Listing 4 Determining whether the device allows tracks to be added to the user’s music library.
let controller = SKCloudServiceController()
controller.requestCapabilitiesWithCompletionHandler({
(capabilities: SKCloudServiceCapability, error: NSError?) in
if capabilities.contains(SKCloudServiceCapability.AddToCloudMusicLibrary)
{
print("The device allows tracks to be added to the user’s music library.")
}
})
Document Revision History
Date | Notes |
---|---|
2016-07-05 | New document that describes how to determine your device's music library capabilities using SKCloudServiceController. |
Copyright © 2016 Apple Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2016-07-05