SoFunction
Updated on 2025-03-02

Detailed explanation of permission processing examples of swift photo album camera

Preface

Recently, I encountered the need to obtain camera permissions for albums in development. I finally solved it by looking for relevant information. Because I will use it later, I decided to record it. I won’t say much below. Let’s take a look at the detailed introduction together.

Notice:Must be inAdd two keyNSPhotoLibraryUsageDescription, and NSCameraUsageDescription to the content, fill in the content and prompt the content.

camera

// use(authorizedBlock: {
 print("Open the camera")
}, deniedBlock: {
 print("No permission to use the camera")
})

// Camera permissionsclass func cameraPermissions(authorizedBlock: OperationBlock?, deniedBlock: OperationBlock?) {
 let authStatus = (for: )
 
 // .notDetermined .authorized .restricted .denied
 if authStatus == .notDetermined {
  // The first time the authorization is triggered alert  (for: .video, completionHandler: { (granted: Bool) in
   (authorizedBlock: authorizedBlock, deniedBlock: deniedBlock)
  })
 } else if authStatus == .authorized {
  if authorizedBlock != nil {
   authorizedBlock!()
  }
 } else {
  if deniedBlock != nil {
   deniedBlock!()
  }
 }
}

Photo Album

ALAssetsLibrary after iOS9.0 is outdated, and use PHPhotoLibrary

// use(authorizedBlock: {
 print("Open Album")
}, deniedBlock: {
 print("No permission to open the album")
})

// Album permissionsclass func photoAlbumPermissions(authorizedBlock: OperationBlock?, deniedBlock: OperationBlock?) {
 let authStatus = ()
 
 // .notDetermined .authorized .restricted .denied
 if authStatus == .notDetermined {
  // The first time the authorization is triggered alert   { (status:PHAuthorizationStatus) -> Void in
   (authorizedBlock: authorizedBlock, deniedBlock: deniedBlock)
  }
 } else if authStatus == .authorized {
  if authorizedBlock != nil {
   authorizedBlock!()
  }
 } else {
  if deniedBlock != nil {
   deniedBlock!()
  }
 }
}

Summarize

The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.