Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 914
ajay_nasa Avatar asked Aug 19 '16 07:08

ajay_nasa


2 Answers

-(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;
            }
        }
    }];
}
like image 142
Marco Santarossa Avatar answered Sep 18 '22 15:09

Marco Santarossa


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.

like image 40
ajay_nasa Avatar answered Sep 19 '22 15:09

ajay_nasa