Xcode 12中的开关必须详尽无遗

  • 本文关键字:开关 Xcode swift xcode12
  • 更新时间 :
  • 英文 :


在新的Xcode 12中,我在权限范围中遇到了一个特殊的错误。开关必须是详尽的add.limited case虽然我添加了有限的情况,但它仍然显示了这个错误。我在使用开关盒的地方得到了这个。你能给我指对方向吗。我最近把我的项目从Xcode 11升级到了Xcode 12,所以这里有点困惑。

func openPhotoController(item: InfoItem) {


let status = PHPhotoLibrary.authorizationStatus()

switch status {
case .authorized:

DispatchQueue.main.async
{
let photoLib = photoLibViewController()
photoLib.delegate = self
photoLibCropImage = false
photoLib.modalTransitionStyle = .crossDissolve
photoLib.modalPresentationStyle = .fullScreen
photoLib.allowMultipleSelection = false
self.present(photoLib, animated: true, completion: nil)
}
print("authorized")
//handle authorized status
case .denied, .restricted :

print("denied")

AlertManager.showAlertView(title: "Alert?", subtitle: "Please allow access to your photo library to use this functionality", showCancelButton: true, okButtonTitle: "Go to Settings", cancelButtonTitle: "Cancel") {

if let url = URL(string:UIApplication.openSettingsURLString)
{
if UIApplication.shared.canOpenURL(url)
{
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
}
//handle denied status
case .notDetermined:
// ask for permissions
PHPhotoLibrary.requestAuthorization { status in
switch status {
case .authorized:

print("authorized")

DispatchQueue.main.async
{
let photoLib = photoLibViewController()
photoLib.delegate = self
photoLibCropImage = false
photoLib.modalTransitionStyle = .crossDissolve
photoLib.modalPresentationStyle = .fullScreen
photoLib.allowMultipleSelection = false
self.present(photoLib, animated: true, completion: nil)
}
// as above
case .denied, .restricted:

print("denied")
// as above
case .notDetermined:
print("notDetermined")
// won't happen but still
case .limited:
print("notDetermined")
default:
print("notDetermined")
}  
}
}
}

WWDC 2020中,iOS 14苹果推出了一项新功能,该功能将限制对照片库的访问。您缺少外部主交换机的.limited机箱。您的更新代码:

switch PHPhotoLibrary.authorizationStatus(for: .readWrite) {
case .notDetermined:
// ask for access
case .restricted, .denied:
// sorry
case .authorized:
// we have full access

// new option: 
case .limited:
// we only got access to a part of the library
}

更多信息,您可以在这里查看

为外部交换机添加.limited大小写。

如果你不想处理它,你可以总是插入一个默认情况(你有一个内部开关,但没有外部开关(:

...
default:
// Otherwise, do something else

Swift开关上的文档:https://docs.swift.org/swift-book/LanguageGuide/ControlFlow.html.

相关内容

  • 没有找到相关文章

最新更新