11

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.

enter image description here

ajay_nasa
  • 2,278
  • 2
  • 28
  • 45
  • Possible duplicate of [Ask permission to access Camera Roll](http://stackoverflow.com/questions/13572220/ask-permission-to-access-camera-roll) – rptwsthi Aug 19 '16 at 07:55
  • 2
    @rptwsthi, please read question carefully permission from camera roll and media player is not same. – ajay_nasa Aug 19 '16 at 07:57
  • You mentioned media library in your question not player. Plus check accepted answer to the question that should solve your problem: http://stackoverflow.com/a/13572568/656600 – rptwsthi Aug 19 '16 at 08:04
  • 1
    @rptwsthi, Please install any app in your device who access your media library(music files) and camera roll too. And then open setting you will get know these both are different permission. – ajay_nasa Aug 19 '16 at 08:13

3 Answers3

21
-(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;
            }
        }
    }];
}
ajay_nasa
  • 2,278
  • 2
  • 28
  • 45
Marco Santarossa
  • 4,058
  • 1
  • 29
  • 49
3

Temporarily, i solved my problem by checking songArray object in below code

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. 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.

ajay_nasa
  • 2,278
  • 2
  • 28
  • 45
1

Swift 4 access check. 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; 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

App Dev Guy
  • 5,396
  • 4
  • 31
  • 54
  • 1
    Man I wish people would comment why they vote a solution down. This looks good to me, now I'm unsure if this will work or not or what the issue might be. – TheJeff May 03 '20 at 18:13