Ios media library resolution detection

In my application, I want to find out if the user gives permission to their media library or not. The user can refuse permission of the multimedia library at the request of the system or after its installation. Is there a way to determine the permission status of a media library?

Here is my code that has access to the list of songs.

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

Below is a screenshot where the user can change the permissions of the media library.

enter image description here

+6
source share
3 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; } } }]; } 
+17
source

I temporarily solved my problem by checking the songArray object in the lower code

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

If the user denied permission, then the songArray object is always zero, but if the user allows access to the Media Library , then the songArray object has an array of songs. Even if there are no songs in the device, but the user gives permission to access the Media Library , then there will be an array with the number 0.

+3
source

Swift 4 . A simple solution is as follows, and you can change it to include other alternatives, but in my case it was access or nothing.

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

Caution about using the above solutions - they are executed as a block statement and do not return a value ( return true or return "authorised" ) in the same thread; the result is processed in the background thread. If you decide to use the above sentences, use a handler (call another function) to handle the expected result. This decision, on the other hand, immediately tells you whether you have access or not. No waiting required.

Additional information is available at Apple Docs.

-one
source

All Articles