JLT JLT - 23 days ago 5
iOS Question

iOS - Getting Callback On Media Library Access Alert By System

enter image description here

I need to reload the music list in my app when the user allows the access to media library, and I would like to show a notification when the user disallows the access. How can I get the two callbacks?

For requesting access, I just added this line into the info.plist

NSAppleMusicUsageDescription

Answer

You need to ask for the permission for accessing MPMediaLibrary, this is how you can request for it

Objective-C

[MPMediaLibrary requestAuthorization:^(MPMediaLibraryAuthorizationStatus authorizationStatus) {
        if ( authorizationStatus == MPMediaLibraryAuthorizationStatusAuthorized ) {
        // Reload your list here
        } else {
        // user did not authorize
} }];

Swift

MPMediaLibrary.requestAuthorization { (status) in
        if status == .authorized {
            // Reload your list here
        } else {
            // user did not authorize
        }
    }
Comments