前言.我要讲的
App Music
是StoreKit框架下的Apple Music
.我可没那么多闲心去讲Apple Music
应用怎么用…
首先,这是一个可以让用户去订阅Apple Music
与提供对Apple Music
库访问的框架(PS.国内用的人挺少的).
是的.类比于相册应用.既然是访问App的原生应用.那么就应该需要权限
在info.plist
中加入以下字段:
Privacy - Media Library Usage Description
(主体是:NSAppleMusicUsageDescription
字段).然后写上一些说明就OK.
请求权限是通过
SKCloudServiceController
类来呼出的
+ (SKCloudServiceAuthorizationStatus)authorizationStatus;
+ (void)requestAuthorization:(void(^)(SKCloudServiceAuthorizationStatus status))handler;
上面一个方法来拿当前的授权状态.下面的一个既可以请求也可以获取状态
当位移枚举capabilities中含有SKCloudServiceCapabilityMusicCatalogSubscriptionEligible
以及不含SKCloudServiceCapabilityMusicCatalogPlayback
时就可以呼出了
[[[SKCloudServiceController alloc] init] requestCapabilitiesWithCompletionHandler:^(SKCloudServiceCapability capabilities, NSError * _Nullable error) {
if (((capabilities&SKCloudServiceCapabilityMusicCatalogPlayback)!=SKCloudServiceCapabilityMusicCatalogPlayback)) {
// 呼出Apple Music
}
}];
用代理方法的话需要遵循<SKCloudServiceSetupViewControllerDelegate>
SKCloudServiceSetupViewController *vc = [[SKCloudServiceSetupViewController alloc] init];
vc.delegate = self;
[vc loadWithOptions:@{SKCloudServiceSetupOptionsActionKey: SKCloudServiceSetupActionSubscribe} completionHandler:^(BOOL result, NSError * _Nullable error) {
}];
[self presentViewController:vc animated:YES completion:nil];
/// Sent when the view controller was dismissed.
- (void)cloudServiceSetupViewControllerDidDismiss:(SKCloudServiceSetupViewController *)cloudServiceSetupViewController __TVOS_PROHIBITED NS_AVAILABLE_IOS(10_1) {
}
SKCloudServiceAuthorizationStatus:
typedef NS_ENUM(NSInteger, SKCloudServiceAuthorizationStatus) {
/// 不确定
SKCloudServiceAuthorizationStatusNotDetermined,
/// 拒绝
SKCloudServiceAuthorizationStatusDenied,
/// 限制(可能开了限制模式)
SKCloudServiceAuthorizationStatusRestricted,
/// 已认证(有权限)
SKCloudServiceAuthorizationStatusAuthorized,
} NS_ENUM_AVAILABLE_IOS(9_3);
typedef NS_OPTIONS(NSUInteger, SKCloudServiceCapability) {
/// 该设备不允许播放苹果音乐内容或向音乐库添加曲目。
SKCloudServiceCapabilityNone = 0,
/// 该设备允许播放苹果音乐目录的曲目。
SKCloudServiceCapabilityMusicCatalogPlayback = 1 << 0,
/// 该设备允许订阅苹果音乐目录。
SKCloudServiceCapabilityMusicCatalogSubscriptionEligible NS_ENUM_AVAILABLE_IOS(10_1) = 1 << 1,
/// 该设备允许将曲目添加到用户的音乐库中。
SKCloudServiceCapabilityAddToCloudMusicLibrary = 1 << 8,
} NS_AVAILABLE_IOS(9_3);