How do I setup Music Kit authorisation?

Hi,

Please see my code below:

`   func configureRequestButton() {

        let button = UIButton()

        button.setTitle("Test", for: .normal)

        button.setTitleColor(.systemBlue,for: .normal)

        button.addTarget(self,

                         action: #selector(button), <---- need the fuction to execute here

                         for: .touchUpInside)

        StackView.addArrangedSubview(button)

    }

    



    

    func requestMusicAuthorization() async throws {

        

        let authorizationStatus = await MusicAuthorization.request()

        if authorizationStatus == .authorized {

            isAuthorizedForMusicKit = true

        } else {

            // User denied permission.

        }

    }````


So I need to basically call that request music authorisation function in that test button but every time I do it says you have to conform it to object c if I do it crashes the app. I have done the Plist stuff. I am using UIKIt programmatically coding the UI. Can someone help because most of the api docs is based for swifUI for music kit. 

You can update your method to handle the errors and concurrency, and then add it as the target:

 button.addTarget(self,
              action: #selector(requestMusicAuthorization),
              for: .touchUpInside)

@objc func requestMusicAuthorization() {
  Task {
   do {
    let authorizationStatus = await MusicAuthorization.request()
    if authorizationStatus == .authorized {

     isAuthorizedForMusicKit = true

    } else {

     // User denied permission.
    }
   } catch {
    print(error)
   }
  }
 }

If this does not work, can you tell what is the error you get when the app crashes?

Hi,

Thanks for your reply. So I tried this and nothing happens. No crashes nothing. It does perform anything.

As per the document, I have added the Plist permission with a text to say why I need permission. This plist thing is supposed to automatically display a alert to accept or deny permission when the app launches as per the doc and this function is supposed to request user permission as a button like Connect Apple Music -> User permission granted and so forth... Except the problem is the original code from Apple dev tutorial is for SwiftUI. I need Uikit that's where I am struggling.

Here is the code from the tutorial:

@State var isAuthorizedForMusicKit = false

func requestMusicAuthorization() {
    detach {
        let authorizationStatus = await MusicAuthorization.request()
        if authorizationStatus == .authorized {
            isAuthorizedForMusicKit = true
        } else {
            // User denied permission.
        }
    }
}

here is my version of it with your help with the button:

 func configureRequestButton() {

        let button = UIButton()

        button.setTitle("Test", for: .normal)

        button.setTitleColor(.systemBlue,for: .normal)

        button.addTarget(self,

                         action: #selector(requestMusicAuthorization),

                         

                         for: .touchUpInside)

        StackView.addArrangedSubview(button)

    }

    



    

    @objc func requestMusicAuthorization() {

      Task {

       do {

        let authorizationStatus = await MusicAuthorization.request()

        if authorizationStatus == .authorized {



         isAuthorizedForMusicKit = true



        } else {



         // User denied permission.

        }

       } catch {

        print(error)

       }

      }

     }

I hope this helps with What I am trying to achieve here. It should only show when the user launches the app for the first time. After we get persimision it doesn't need to show again until they deny permission.

Best, Imran

How do I setup Music Kit authorisation?
 
 
Q