1

Im checking the response the user gives when prompted for accessing the media-library. I want to know how can i wait for the response of the user before continuing

static func getAuthrization(completionHandler:@escaping (Bool) -> Void)  {
        var authStatus:Bool=false;
        let status = MPMediaLibrary.authorizationStatus()
        if(status == MPMediaLibraryAuthorizationStatus.authorized){
            authStatus = true
        }else{
            MPMediaLibrary.requestAuthorization() { status in
                if status == .authorized {
                    authStatus = true
                }else{
                    print("Auth not granted")
                    authStatus = false
                }
            }
        }
        completionHandler(authStatus)
    }

This is the function that i use to get user authentication.

func setSongs(){
        print("called from set songs")
        songQuery.getAuthrization { (status) in
            print(status)
        }
    }

And this is the function that i use to call that function

right now the function gets executed and finished before the user response from the system permission prompt. What i want is the for the code to await like in js or dart before executing and then get the response.

Any help is appreciated

SujithaW
  • 440
  • 5
  • 16
  • Isnt it because you call `completionHandler(authStatus)` outside of the `MPMediaLibrary.requestAuthorization()` completion handler ? – Olympiloutre Nov 12 '19 at 04:53
  • when i did that function get executed and completes it wont return a response at all @Olympiloutre – SujithaW Nov 12 '19 at 05:00

1 Answers1

1

Call completionHandler as below,

static func getAuthrization(completionHandler:@escaping (Bool) -> Void)  {
        if MPMediaLibrary.authorizationStatus() == .authorized {
            completionHandler(true)
        } else {
            MPMediaLibrary.requestAuthorization() { completionHandler($0 == .authorized) }
        }
    }
Kamran
  • 14,987
  • 4
  • 33
  • 51
  • 1
    ah damn that worked like a charm thanks will accept answer after the time limit appreciate for answering so quick – SujithaW Nov 12 '19 at 05:03