简体   繁体   English

MPMediaLibrary requestAuthorization 确实在用户接受权限后执行代码,即使在使用完成处理程序之后

[英]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:问题:

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您遇到的问题与重新加载数据有关,因为授权已正确配置,我可以从您的代码中看到它,所以我建议在视图控制器的生命周期方法中刷新数据,并在 viewWillAppear 中调用授权请求(_动画:布尔)function,这是我给你的推荐

 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.在这里,我所做的是添加计时器以在第二个时间间隔内检查数据,因此在该 scheduleTimer 中,我有选择器 function 来刷新数据存储或数据项,如果数据不存在,我将在其中重新初始化数据,然后我使计时器,也刷新了我的看法。 I have tested it with simple random data implementation, it worked for me using your function to that i request the authorization.我已经用简单的随机数据实现对其进行了测试,它使用你的 function 对我有用,我请求授权。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在动画后不使用完成块的情况下执行代码? - How to execute code after an animation without using the completion block? 如何等待 MPMediaLibrary requestAuthorization 响应 - How to wait for MPMediaLibrary requestAuthorization response 调用完成处理程序后执行的代码 - Code being executed after completion handler is called MPMediaLibrary.requestAuthorization在Xcode 10.2模拟器上没有响应 - MPMediaLibrary.requestAuthorization not responding on Xcode 10.2 simulator 为什么即使在指定返回 - 后台线程到主线程问题后代码也会执行? - Why does code execute even after specifying return - background thread to main thread issue? 在DispatchGroup中执行完成处理程序 - Execute Completion Handler in DispatchGroup 块方法完成后执行方法 - Execute a method after the completion of a block method 即使在完成处理程序中,Swift代码也将异步执行 - Swift code being executed asynchronously even while in completion handler 完成另一个方法后执行一个方法 - Execute a method after the completion of another method 在Facebook发布后,SLComposeViewController完成处理程序不触发(尽管可以在取消状态下工作) - SLComposeViewController completion handler not firing after Facebook post (works on cancel though)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM