Apple在StoreKit框架内提供了一种检测用户是否有Apple Music订阅的方法,尽管这种方法根本行不通。我有Apple Music订阅,既可以在我的应用程序中收听歌曲,也可以从我的应用程序中将内容添加到我的音乐库中。这是我的代码:
let serviceController = SKCloudServiceController()
serviceController.requestCapabilities { (capability:SKCloudServiceCapability, err:Error?) in
print("error in capability check is \(err)")
switch capability {
case SKCloudServiceCapability.musicCatalogPlayback:
print("user has Apple Music subscription and can play music from apple music api")
case SKCloudServiceCapability.addToCloudMusicLibrary:
print("user has an Apple Music subscription, can play music from api, also can add to their music library")
case SKCloudServiceCapability.musicCatalogSubscriptionEligible:
print("user does not have subscription")
default:
print("default and capability is \(capability.rawValue)")
}
}
每次运行我的应用程序时,它都会显示“默认值,功能为257”。我尝试卸载/重新安装大概十几次,每次都一样。
注意,我确实可以获取用户令牌,并通过Apple Music API将歌曲添加到我的音乐库中。
其他人都经历过这种情况,还有另一种变通办法来查看用户是否有Apple Music订阅吗?
答案 0 :(得分:1)
capability
是OptionSet
,因为用户可能具有musicCatalogPlayback
或addToCloudMusicLibrary
或两者都有。
您看到的原始值是实际值的按位OR
,因为正如您所说的,您具有这两种功能。
您可以使用一系列if
语句或特定的if
语句来检查所需的功能:
let serviceController = SKCloudServiceController()
serviceController.requestCapabilities { (capability:SKCloudServiceCapability, err:Error?) in
guard err == nil else {
print("error in capability check is \(err!)")
return
}
if capability.contains(SKCloudServiceCapability.musicCatalogPlayback) {
print("user has Apple Music subscription and can play music from apple music api")
}
if capability.contains(SKCloudServiceCapability.addToCloudMusicLibrary) {
print("user has an Apple Music subscription, can play music from api, also can add to their music library")
}
if capability.contains(SKCloudServiceCapability.musicCatalogSubscriptionEligible) {
print("user does not have subscription")
}
}
给予
用户已订阅Apple Music,并且可以播放来自Apple Music API的音乐
用户具有Apple Music订阅,可以播放api中的音乐,也可以添加到其音乐库中
答案 1 :(得分:0)
您可以使用以下功能来检查Apple Music订阅不起作用/用户没有订阅。
var cloudServiceController:SKCloudServiceController? var cloudServiceSetUpController:SKCloudServiceSetupViewController?
内部函数appleMusicRequestCapabilities(){
if let aController = AppleMusicSharedInstance.shared.cloudServiceController{
aController.requestCapabilities { (capabilities, error) in
if error != nil{
debugPrint("Error", error ?? "Unexpected Error")
}else{
if capabilities.contains(SKCloudServiceCapability.musicCatalogPlayback){
debugPrint("musicCatalogPlayback")
}
if capabilities.contains(SKCloudServiceCapability.musicCatalogSubscriptionEligible){
debugPrint("musicCatalogSubscriptionEligible")
DispatchQueue.main.async {
self.setUpSubscriptionView()
}
}
if capabilities.contains(SKCloudServiceCapability.addToCloudMusicLibrary){
debugPrint("addToCloudMusicLibrary")
}
}
}
}
}
以下方法可用于显示音乐订阅视图,用户可以在其中选择订阅。
内部功能setUpSubscriptionView(){
if let aController = AppleMusicSharedInstance.shared.cloudServiceSetUpController{
aController.load(options: [SKCloudServiceSetupOptionsKey.action: SKCloudServiceSetupAction.subscribe], completionHandler: { (success, error) in
DispatchQueue.main.async {
if success{
AppDelegate.delegate().window?.rootViewController?.present(aController, animated: true, completion: nil)
}
else if (error != nil){
debugPrint("Error", error ?? "Unexpected Error")
}
}
})
}
}