How may I check if my app has access to phone gallery

前端 未结 4 1415
伪装坚强ぢ
伪装坚强ぢ 2021-02-05 10:58

I have an app in which I take a picture with the camera and store that image into the native gallery. But if the app doesn\'t have permission for that, I want the user to know t

相关标签:
4条回答
  • 2021-02-05 11:33

    You need to check the status of ALAssetLibrary make sure you have AssetsLibrary/AssetsLibrary.h included in your file

      ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];
    

    // check the status for ALAuthorizationStatusAuthorized or ALAuthorizationStatusDenied e.g

        if (status != ALAuthorizationStatusAuthorized) {
            //show alert for asking the user to give permission
    
        }
    
    0 讨论(0)
  • 2021-02-05 11:46

    Note: iOS 6 Only

    Is this what you are looking for

    [ALAssetsLibrary authorizationStatus] == ALAuthorizationStatusAuthorized;
    

    Other values of authorizationStatus are

    ALAuthorizationStatusRestricted,        // This application is not authorized to access photo data.
                                                // The user cannot change this application’s status, possibly due to active restrictions
                                                //  such as parental controls being in place.
        ALAuthorizationStatusDenied,            // User has explicitly denied this application access to photos data.
        ALAuthorizationStatusAuthorized         // User has authorized this application to access photos data.
    
    0 讨论(0)
  • 2021-02-05 11:53

    If you are using photos framework since ALAsset libraries are deprecated from ios 9 you can use PHAuthorizationStatus to check gallery access. You need to import photos framework as well.

      #import <Photos/Photos.h>
    
    - (BOOL)hasGalleryPermission
    {
        BOOL hasGalleryPermission = NO;
        PHAuthorizationStatus authorizationStatus = [PHPhotoLibrary authorizationStatus];
    
        if (authorizationStatus == PHAuthorizationStatusAuthorized) {
            hasGalleryPermission = YES;
        }
        return hasGalleryPermission;
    }
    
    0 讨论(0)
  • 2021-02-05 11:56

    Swift 3

    import photos
    
    PHPhotoLibrary.requestAuthorization { status in
         switch status {
         case .authorized:
              self.processSnapShotPhotos()
         case .restricted:
              print("handle restricted")
         case .denied:
              print("handle denied")     
         default:
           // place for .notDetermined - in this callback status is already determined so should never get here
                break
         }
    }
    
    0 讨论(0)
提交回复
热议问题