我正在使用以下代码检查 MPMediaLibrary 授权:
func handlePermissions() {
let permissionStatus = MPMediaLibrary.authorizationStatus()
switch (permissionStatus) {
case MPMediaLibraryAuthorizationStatus.authorized:
print("permission status is authorized")
case MPMediaLibraryAuthorizationStatus.notDetermined:
print("permission status is not determined")
MPMediaLibrary.requestAuthorization(MPMediaLibraryAuthorizationStatus -> permissionStatus)
case MPMediaLibraryAuthorizationStatus.denied:
print("permission status is denied")
case MPMediaLibraryAuthorizationStatus.restricted:
print("permission status is restricted")
}
}
最终,我试图在调用查询之前提示用户授权(在启动时)......通过案例 MPMediaLibraryAuthorizationStatus.notDetermined:。上面的代码产生错误:“->”之后的预期类型。注释掉 requestAuthorization() 行时,应用程序在启动时崩溃(访问尚未授权),并且在启动屏幕消失后显示授权提示视图。
我已经看到了一些如何在 Objective C 中执行 requestAuthorization() 的示例,但在 Swift 中没有。我不明白:
MPMediaLibrary.requestAuthorization( handler: (MPMediaLibraryAuthorizationStatus) -> Void )
在 Swift 3 中请求访问 MPMediaLibrary 的正确方法是什么?