1

在 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.");
        }

    }];

}];
4

1 回答 1

4

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

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

于 2016-11-14T18:14:23.217 回答