Detect permission of media library ios

后端 未结 3 526
闹比i
闹比i 2020-12-11 02:27

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 settin

相关标签:
3条回答
  • 2020-12-11 03:06
    -(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;
                }
            }
        }];
    }
    
    0 讨论(0)
  • 2020-12-11 03:09

    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.

    0 讨论(0)
  • 2020-12-11 03:10

    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

    0 讨论(0)
提交回复
热议问题