Determine if the access to photo library is set or not - PHPhotoLibrary

后端 未结 11 827
误落风尘
误落风尘 2020-11-27 10:23

With the new functionality in iOS 8, if you are using a camera in the app, it will ask for permission to access the camera and then when you try to retake the pic, it asks f

11条回答
  •  死守一世寂寞
    2020-11-27 10:54

    Just as formality, Swift 2.X version:

        func checkPhotoLibraryPermission() {
           let status = PHPhotoLibrary.authorizationStatus()
           switch status {
           case .Authorized:
                //handle authorized status
           case .Denied, .Restricted :
                //handle denied status
           case .NotDetermined:
                // ask for permissions
                PHPhotoLibrary.requestAuthorization() { (status) -> Void in
                   switch status {
                   case .Authorized:
                       // as above
                   case .Denied, .Restricted:
                       // as above
                   case .NotDetermined:
                       // won't happen but still
                   }
                }
            }
        }
    

    And Swift 3 / Swift 4:

        import Photos
    
        func checkPhotoLibraryPermission() {
            let status = PHPhotoLibrary.authorizationStatus()
            switch status {
            case .authorized: 
            //handle authorized status
            case .denied, .restricted : 
            //handle denied status
            case .notDetermined: 
                // ask for permissions
                PHPhotoLibrary.requestAuthorization { status in
                    switch status {
                    case .authorized: 
                    // as above
                    case .denied, .restricted: 
                    // as above
                    case .notDetermined: 
                    // won't happen but still
                    }
                }
            }
        }
    

提交回复
热议问题