2

I am using the following code to check the MPMediaLibrary authorizations:

func handlePermissions() {
    let permissionStatus = MPMediaLibrary.authorizationStatus()
    switch (permissionStatus) {
    case MPMediaLibraryAuthorizationStatus.authorized:
        print("permission status is authorized")
    case MPMediaLibraryAuthorizationStatus.notDetermined:
        print("permission status is not determined")
        MPMediaLibrary.requestAuthorization(MPMediaLibraryAuthorizationStatus -> permissionStatus)
    case MPMediaLibraryAuthorizationStatus.denied:
        print("permission status is denied")
    case MPMediaLibraryAuthorizationStatus.restricted:
        print("permission status is restricted")
    }
 }

Ultimately, I am trying to prompt the user for their authorization (upon launch) prior to calling a query...via the case MPMediaLibraryAuthorizationStatus.notDetermined:. The code above produces the error: Expected type after '->'. When the requestAuthorization() line is commented out, the app crashes upon launch (access has not been authorized) and the authorization prompt view is shown after the launch screen disappears.

I've seen some examples of how to perform requestAuthorization() in Objective C but nothing in Swift. I don't understand:

 MPMediaLibrary.requestAuthorization( handler: (MPMediaLibraryAuthorizationStatus) -> Void )

What is the proper way to request authorization for access to the MPMediaLibrary in Swift 3?

rocketman240
  • 119
  • 12

2 Answers2

2

You've actually used the prototype of the requestAuthorization method. You need to adapt it to your own use.

MPMediaLibrary.requestAuthorization( handler: (MPMediaLibraryAuthorizationStatus) -> Void )

means that requestAuthorization take a function as parameter and this function takes a MPMediaLibraryAuthorizationStatus as parameter an return nothing.

For example if I want to request the authorisation and then display the result inside my console. I first check if the application is not already authorised :

if authoriationStatus != .authorized {
        MPMediaLibrary.requestAuthorization({
            (status) in
            switch status {
            case .notDetermined:
                print("notDetermined")
            case .denied:
                print("denied")
            case .restricted:
                print("restricted")
            case .authorized:
                print("authorized")
            }
        })
    }

As you can see, I used a function as a parameter for the method requestAuthorization. The function is described inside {...}. It's called a closure and it's something you always use in Swift.

Asphel
  • 21
  • 2
2

For swift 4.2 to check authorisations for MPMediaLibrary

import MediaPlayer

let status = MPMediaLibrary.authorizationStatus()
        switch status {
        case .authorized:
            self.openMusicLibrary()
            break
        case .notDetermined:
            MPMediaLibrary.requestAuthorization() { status in
                if status == .authorized {
                    DispatchQueue.main.async {
                        self.openMusicLibrary()
                    }
                }
            }
            break
        case .denied:
            //show alert
            print("Please Allow Access to the Media & Apple Music from appliction setting.")
            break
        case .restricted:
            break
        }
Hardik Thakkar
  • 15,269
  • 2
  • 94
  • 81