Search code examples
swiftxcode12

Switch must be exhaustive in Xcode 12


in new Xcode 12, I am getting a peculiar error in permission scope. Switch must be exhaustive add .limited case. Though I added .limited case its still showing this error. I am getting this on where ever switch cases is used. Can You point me in the right direction. I recently upgraded my project from Xcode 11 to Xcode 12 so am a bit confused here.

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")
            }  
        }
    }
}

Solution

  • In WWDC 2020, iOS 14 Apple introduced a new feature that will give limited access to the photo library. You are missing .limited case for outer main Switch. Your updated code:

    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
    }
    

    For More, you can check here