Apple Music detect是会员

时间:2016-06-03 12:02:30

标签: objective-c ios9.3 apple-music

在iOS 9.3中,Apple发布了新的API。现在,开发人员可以查看用户当前是否是Apple Music的成员。我试图了解它是如何工作的。 我的代码 - 只是第一次 - 询问用户是否访问音乐库,但我不知道如何确定您可以检测用户是否是其成员的方式并打开Apple Music加入它。 Shazam的这些行动非常棒。我怎么能这样做? 提前谢谢!

[SKCloudServiceController requestAuthorization:^(SKCloudServiceAuthorizationStatus status) {

    NSLog(@"status is %ld", (long)status);

    SKCloudServiceController *cloudServiceController = [[SKCloudServiceController alloc] init];

    [cloudServiceController requestCapabilitiesWithCompletionHandler:^(SKCloudServiceCapability capabilities, NSError * _Nullable error) {

        NSLog(@"%lu %@", (unsigned long)capabilities, error);

        if (capabilities >= SKCloudServiceCapabilityAddToCloudMusicLibrary || capabilities==SKCloudServiceCapabilityMusicCatalogPlayback) {

            NSLog(@"You CAN add to iCloud!");


        } else {

            NSLog(@"The ability to add Apple Music track is not there. sigh.");
        }

    }];

}];

1 个答案:

答案 0 :(得分:4)

您应该检查功能中的SKCloudServiceCapabilityMusicCatalogPlayback标志。您的问题中的代码检查capabilities变量是否等于该标志,但您需要使用按位运算符。这就是你要怎么做的。

SKCloudServiceController *controller = [SKCloudServiceController new];
[controller requestCapabilitiesWithCompletionHandler:^(SKCloudServiceCapability capabilities, NSError * _Nullable error) {
    if (error != nil) {
        NSLog(@"Error getting SKCloudServiceController capabilities: %@", error);

    } else if (capabilities & SKCloudServiceCapabilityMusicCatalogPlayback) {
        // The user has an active subscription

    } else {
        // The user does *not* have an active subscription
    }
}];

您可以告诉SKCloudServiceCapabilityMusicCatalogPlayback是一个标志,而不仅仅是一个常规常量值,因为该值使用按位运算符(Apple的文档中显示的“<<”)。

SKCloudServiceCapabilityMusicCatalogPlayback = 1 << 0

https://developer.apple.com/reference/storekit/skcloudservicecapability/skcloudservicecapabilitymusiccatalogplayback?language=objc