Search code examples
iosobjective-cswiftmedia-library

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.

enter image description here


Solution

  • -(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;
                }
            }
        }];
    }