2017-04-20 89 views
1

我試圖調用此函數來檢查Apple Music訂閱狀態。我有一個積極的訂閱,並在我的iPhone上聽音樂。 但是當我正在運行測試應用程序時,capability值無效。Swift 3.1 SKCloudServiceCapability問題

它應該是SKCloudServiceCapability.musicCatalogPlayback,SKCloudServiceCapability.addToCloudMusicLibrary或未設置。我只能得到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


這裏有什麼錯?

+0

在蘋果開發論壇上提問相同: https://forums.developer.apple.com/message/228739#228739 –

回答

2

最後來自Apple論壇的人給了我這個鏈接到文檔,我發現這個問題。 https://developer.apple.com/library/content/qa/qa1929/_index.html

我應該使用如果capability.contains(SKCloudServiceCapability。)而不是開關的能力值。 所以這段代碼工作得很好。

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

     } 
    } 
}