在 iOS 中,可以使用 PHPhotoLibrary 类来判断是否有相册权限。以下是一个示例代码:

import Photos

func checkPhotoLibraryPermission() {
    let status = PHPhotoLibrary.authorizationStatus()
    
    switch status {
    case .authorized:
        // 有相册权限
        print('有相册权限')
    case .denied, .restricted:
        // 无相册权限
        print('无相册权限')
    case .notDetermined:
        // 尚未确定是否有相册权限,可以请求权限
        PHPhotoLibrary.requestAuthorization { (status) in
            if status == .authorized {
                // 已授权
                print('已授权')
            } else {
                // 未授权
                print('未授权')
            }
        }
    @unknown default:
        break
    }
}

// 调用方法
checkPhotoLibraryPermission()

在上述代码中,首先使用 PHPhotoLibrary.authorizationStatus() 方法获取当前相册权限的状态。然后,根据不同的状态进行相应的处理。如果状态为 authorized,则表示有相册权限;如果状态为 deniedrestricted,则表示无相册权限;如果状态为 notDetermined,则表示尚未确定是否有相册权限,可以使用 PHPhotoLibrary.requestAuthorization() 方法请求权限。

iOS 相册权限判断方法 - Swift 代码示例

原文地址: https://www.cveoy.top/t/topic/p2hA 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录