[英]MPMediaLibrary requestAuthorization does execute the code after user accept the permission , Even after using completion 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
}
}
我什至試過這個:
func getAuthrization(completionHandler:@escaping (Bool) -> Void) {
if MPMediaLibrary.authorizationStatus() == .authorized {
completionHandler(true)
} else {
MPMediaLibrary.requestAuthorization() { completionHandler($0 == .authorized) }
}
}
但是他們兩個都沒有幫助我,彈出窗口會顯示,但是在接受后,它沒有調用回調,也沒有刷新我的視圖來填充數據。我知道這是關於刷新我的視圖,因為當我再次運行它,我得到了結果,並且權限狀態更改為已授權。
問題:
您遇到的問題與重新加載數據有關,因為授權已正確配置,我可以從您的代碼中看到它,所以我建議在視圖控制器的生命周期方法中刷新數據,並在 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)
}
}
在這里,我所做的是添加計時器以在第二個時間間隔內檢查數據,因此在該 scheduleTimer 中,我有選擇器 function 來刷新數據存儲或數據項,如果數據不存在,我將在其中重新初始化數據,然后我使計時器,也刷新了我的看法。 我已經用簡單的隨機數據實現對其進行了測試,它使用你的 function 對我有用,我請求授權。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.