本文介绍了如何检查用户的设备上是否安装了 Apple Music?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在快速制作音乐应用程序.该应用程序允许用户通过他们的 Apple Music 应用程序通过他们的 Apple Music 订阅播放音乐.我可以通过以下方式检查用户是否订阅了 Apple Music:

SKCloudServiceController().requestCapabilities { (capability:SKCloudServiceCapability, err:Error?) in守卫错误==零其他{打印(能力检查中的错误是\(错误!)")返回}如果capability.contains(SKCloudServiceCapability.musicCatalogPlayback){print("用户订阅了 Apple Music")}如果capability.contains(SKCloudServiceCapability.musicCatalogSubscriptionEligible){print("用户没有订阅")}}

但是:在某些情况下,某些人出于某种原因订阅了 Apple Music,但未在其设备上下载 Apple Music 应用.如果用户有订阅但没有设备,我想基本上将这种情况视为他们根本没有订阅,即我们无法通过 Apple Music 播放音乐.

因此,我开始寻找方法来检查 Apple Music 是否在用户的设备上.我找到了这个答案:

那么为什么设备说我可以打开 URL(string: "music://") 即使在 Apple Music 被删除后?URL 能不能打开,结果只是上面提示的提示?这是确认用户在其设备上安装了 Apple Music 的正确方法吗?有没有办法确认用户在他们的设备上安装了 Apple Music?如果 Apple 为用户提供删除 Apple Music 应用的选项,那么他们还应该让开发者能够检查该应用是否已安装.

解决方案

我得到的最好的解决方案是使用 MPMusicPlayer.prepareToPlay(completionHandler:)代码>检查尝试播放曲目时是否有错误:

SKCloudServiceController().requestCapabilities { (capability:SKCloudServiceCapability, err:Error?) in守卫错误==零其他{打印(能力检查中的错误是\(错误!)")返回}如果capability.contains(SKCloudServiceCapability.musicCatalogPlayback){print("用户订阅了 Apple Music")MPMusicPlayerController.systemMusicPlayer.setQueue(with: ["1108845248"])systemMusicPlayer.prepareToPlay { (error) in如果错误 != nil &&error!.localizedDescription == "操作无法完成.(MPCPlayerRequestErrorDomain 错误 1.)" {//看起来用户没有安装Apple Music App}}}如果capability.contains(SKCloudServiceCapability.musicCatalogSubscriptionEligible){print("用户没有订阅")}}

我不确定这如何适用于在他们的应用程序中使用 Apple Music 进行除播放曲目以外的任何其他事情的任何人,但是当您要播放支票时,这似乎绝对可以作为支票.每当我遇到该错误时,我都会创建一个警报,告诉个人他们订阅了 Apple Music 但没有安装该应用.

不过,能够在没有完成处理程序的情况下进行检查会很棒,因为这将允许将布尔检查集成到条件语句中(通过 if capacity.contains(SKCloudServiceCapability.musicCatalogPlayback) && hasAppleMusicAppInstalled{//做某事}).

I am making a music app with swift. The app lets users play music through their Apple Music subscription via their Apple Music app. I am able to check whether the user has an Apple Music subscription via:

SKCloudServiceController().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")
    }

    if capability.contains(SKCloudServiceCapability.musicCatalogSubscriptionEligible) {
        print("user does not have subscription")
    }

}

However: there are scenarios where somebody will, for some reason, have an Apple Music subscription but not have the Apple Music app downloaded on their device. If the user has the subscription but not the device, I want to essentially treat that case as if they do not have a subscription at all, i.e. we cannot play music via Apple Music.

So, I go searching for ways to add a check for if Apple Music is on the user's device. I find this answer: Check whether an app is installed using Swift combined with this resource for finding Apple Music's url scheme and conclude I can check if a user has both an Apple Music subscription and the Apple Music app installed on their device via:

SKCloudServiceController()requestCapabilities { (capability:SKCloudServiceCapability, err:Error?) in

    guard err == nil else {
        print("error in capability check is \(err!)")
        return
    }

    if capability.contains(SKCloudServiceCapability.musicCatalogPlayback) && UIApplication.shared.canOpenURL(URL(string: "music://")!) {
        print("user has Apple Music subscription and has the apple music app installed")
    }

    if capability.contains(SKCloudServiceCapability.musicCatalogSubscriptionEligible) || !UIApplication.shared.canOpenURL(URL(string: "music://")!) {
        print("user does not have subscription or doesn't have apple music installed")
    }

}

The issue is, even after deleting Apple Music from my device, the first case, i.e. the one that prints user has Apple Music subscription and has the apple music app installed is still being called. I believe I have the correct url scheme because when changing "music://" to "musi://", the second case, i.e. the one that prints user does not have subscription or doesn't have apple music installed is being called.

When trying to open URL(string: "music://") with Apple Music deleted via UIApplication.shared.open(URL(string: "music://")!), I am hit with the following alert:

So why is the device saying that I can open URL(string: "music://") even after Apple Music is deleted? Is the URL capable of being opened, but the result is simply the presentation of the above alert? Is this the correct way to confirm that the user has Apple Music installed on their device? Is there even a way to confirm the user has Apple Music installed on their device? If Apple gives users the option to delete the Apple Music app, they should also give developers the ability to check if the app is installed.

解决方案

The best solution I've got, though I expect there is something better out there, is to use MPMusicPlayer.prepareToPlay(completionHandler:) to check if there is an error when trying to play a track:

SKCloudServiceController().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")
        MPMusicPlayerController.systemMusicPlayer.setQueue(with: ["1108845248"])
        systemMusicPlayer.prepareToPlay { (error) in
            if error != nil && error!.localizedDescription == "The operation couldn’t be completed. (MPCPlayerRequestErrorDomain error 1.)" {
                //It would appear that the user does not have the Apple Music App installed
            }
        }
    }

    if capability.contains(SKCloudServiceCapability.musicCatalogSubscriptionEligible) {
        print("user does not have subscription")
    }

}

I am not sure how this could apply to anybody using Apple Music within their app for anything other than playing tracks, but this seems to definitely work as a check when you are about to play a check. Whenever I am hit with that error, I simply create an alert telling the individual they have an Apple Music subscription but doesn't have the app installed.

Still, it would be great to be able to check without some completion handler as that would allow the boolean check to be integrated into conditional statements (via if capability.contains(SKCloudServiceCapability.musicCatalogPlayback) && hasAppleMusicAppInstalled { //do something }).

这篇关于如何检查用户的设备上是否安装了 Apple Music?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 17:50