简体   繁体   English

检测媒体库ios的权限

[英]Detect permission of media library ios

In my app, I want to detect that if user give the permission to his media library or not. 在我的应用程序中,我想检测一下,如果用户是否允许他的媒体库。 User may denied media library permission when system popup ask or later from setting. 当系统弹出询问或稍后设置时,用户可能会拒绝媒体库权限。 Is there any way to detect the status of media library permission? 有没有办法检测媒体库权限的状态?

Here is my code that access list of songs. 这是我访问歌曲列表的代码。

MPMediaQuery *everything = [MPMediaQuery songsQuery];
NSArray *songArray = [everything items];

Please see below screenshot where user can change Media Library permissions. 请参阅下面的屏幕截图,用户可以更改媒体库权限。

在此输入图像描述

-(void) checkMediaLibraryPermissions {
    [MPMediaLibrary requestAuthorization:^(MPMediaLibraryAuthorizationStatus status){
        switch (status) {
            case MPMediaLibraryAuthorizationStatusNotDetermined: {
                // not determined
                break;
            }
            case MPMediaLibraryAuthorizationStatusRestricted: {
                // restricted
                break;
            }
            case MPMediaLibraryAuthorizationStatusDenied: {
                // denied
                break;
            }
            case MPMediaLibraryAuthorizationStatusAuthorized: {
                // authorized
                break;
            }
            default: {
                break;
            }
        }
    }];
}

Temporarily, i solved my problem by checking songArray object in below code 暂时,我通过在下面的代码中检查songArray对象解决了我的问题

MPMediaQuery *everything = [MPMediaQuery songsQuery]; 
NSArray *songArray = [everything items];

If, user denied permission then songArray object is always nil, but if user allows permission to access to Media Library then songArray object have array of songs. 如果,用户拒绝权限,则songArray对象始终为nil,但如果用户允许访问Media LibrarysongArray对象具有一系列歌曲。 Even if there will be no songs in device but user give permission to access Media Library then there will be array with 0 count. 即使设备中没有歌曲但用户允许访问Media Library ,也会有0计数的数组。

Swift 4 access check. Swift 4访问检查。 The simple solution is as follows, and you can alter to include the other alternatives however in my case it was all access or nothing. 简单的解决方案如下,您可以改为包含其他替代方案,但在我的情况下,它是全部访问或什么也没有。

private func checkPermissionForMusic() -> Bool {
    switch MPMediaLibrary.authorizationStatus() {
    case .authorized:
        return true
    default:
        return false
    }
}

Caution about using the above solutions - they do perform as a block statement and do not return a value ( return true or return "authorised" ) on the same thread; 关于使用上述解决方案的注意事项 - 它们确实作为块语句执行,并且不在同一线程上返回值( return truereturn "authorised" ); the result is handled on a background thread. 结果在后台线程上处理。 If you decide to use the suggestions above, use a handler (call another function) to handle the result you're expecting. 如果您决定使用上述建议,请使用处理程序(调用另一个函数)来处理您期望的结果。 This solution on the other hand tells you immediately if you have access or not. 另一方面,此解决方案会立即告诉您是否有权访问。 No waiting required. 无需等待。

More info is available in the Apple Docs Apple Docs中提供了更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM