iospermissionsmpmedialibrary

MPMediaLibrary requestAuthorization does execute the code after user accept the permission , Even after using completion handler


enter code hereI have code like this for the permission handler:

func authorizeMediaLibrary(forStatus status: MPMediaLibraryAuthorizationStatus) -> Void{
    switch status {
    case .authorized:
           self.initializeMedia()
    case .denied:
        guard let settingUrl = URL(string: UIApplication.openSettingsURLString) else {return}
        if UIApplication.shared.canOpenURL(settingUrl) {
            UIApplication.shared.open(settingUrl) { success in}
        }
    case .notDetermined:
        MPMediaLibrary.requestAuthorization { stat -> Void in
       
            if stat == .authorized {
                self.media.getMediaQueryCollection()
                self.allMediaItems = self.media.getMPMediaItemCollection()

            }
        }
    default:
       break
    }
    
}

I have even tried this:

func getAuthrization(completionHandler:@escaping (Bool) -> Void)  {
            if MPMediaLibrary.authorizationStatus() == .authorized {
                completionHandler(true)
            } else {
                MPMediaLibrary.requestAuthorization() { completionHandler($0 == .authorized) }
            }
        } 

But both of them are not helping me, the popup will display, but after accepted, it is not calling the callback and it is not refreshing my view to populate the data.I know it is about refreshing my view from what i understand because when i run it again i get the result and the permission status is changed to authorized.

question:


Solution

  • The issue your having is related with reloading the data, because the authorization is correctly configured as i can see it from your code, so here is my recommendation to refresh the data in the view controller's lifecycle methods, And also call the authorization request inside viewWillAppear(_ animated: Bool) function, here is my recommendation for you

     override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
            authorizeMediaLibrary(forStatus: 
            MPMediaLibrary.authorizationStatus())
            //initialize your code for fetching the music data here.
           // reload your view , for example if you are showing it in tableview  
     use something like this, *self.tableview.reloadData()*
            if mediaItemFetched.count < 1 {
                timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector:     #selector(self.refreshItem(), userInfo: nil, repeats: true)
                
            }
        }
    

    here what i did is i add timer to check within a second time interval for the data , so within that scheduledTimer i have selector function to refresh the data store or data item in which i reinitialize the data if it doesnt exist, then i invalidate the timer and also refresh my view . I have tested it with simple random data implementation, it worked for me using your function to that i request the authorization.