Apple Music Api -- 检测 Apple Music 订阅不工作

Apple Music Api -- Detect Apple Music subscription not working

A​​pple 在 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)")
    }
}

每次我 运行 我的应用程序都会打印 "default and capability is 257." 我已经尝试 uninstalling/reinstalling 大概十几次了——每次都一样。

请注意,我确实可以通过 Apple Music api 获取我的用户令牌并将歌曲添加到我的音乐库中。

是否有其他人遇到过这种情况,是否有其他解决方法来查看用户是否订阅了 Apple Music?

capability 是一个 OptionSet,因为用户可能具有 musicCatalogPlaybackaddToCloudMusicLibrary 或两种能力。

您看到的原始值是实际值的按位 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")
    }
}

给予

user has Apple Music subscription and can play music from apple music api

user has an Apple Music subscription, can play music from api, also can add to their music library

您可以使用以下功能来检查 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")
                }
            }
        })
    }
}