在iOS11中,您无法通过“ALAssetsLibrary”或“PHPhotoLibrary”命令获得权限

时间:2017-09-28 00:33:39

标签: ios alassetslibrary phphotolibrary

当我尝试确定用户是否有权打开相册时,xcode告诉我这不起作用。

let authStatus = ALAssetsLibrary.authorizationStatus()
return authStatus != .restricted && authStatus != .denied

xcode提醒我

  

'使用未解析的标识符'ALAssetsLibrary'

xcode screenshot

当我尝试在AppDelegate上使用'PHPhotoLibrary'时

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.


    switch PHPhotoLibrary.authorizationStatus() {
    case PHAuthorizationStatus.NotDetermined: // 用户暂未权限认证
        print("PHAuthorizationStatus.NotDetermined")
        // 权限认证
        PHPhotoLibrary.requestAuthorization { (status:PHAuthorizationStatus) -> Void in
            print(status)
        }
    case PHAuthorizationStatus.Restricted: // APP禁止使用相册权限认证
        print("PHAuthorizationStatus.Restricted")
    case PHAuthorizationStatus.Denied: // 用户拒绝使用相册
        print("PHAuthorizationStatus.Denied")
        print("请进入 设置 -> 隐私 -> 相册 开启权限")
    // 设置-隐私-相册
    case PHAuthorizationStatus.Authorized: // 用户允许使用相册
        print("PHAuthorizationStatus.Authorized")
    }


    return true
}
xcode还告诉我

  

使用未解析的标识符'PHPhotoLibrary'

1 个答案:

答案 0 :(得分:9)

好的,我已经意识到答案是什么,在一切正常后在swift文件的顶部添加“导入照片”

" PHPhotoLibrary"以下列方式正确使用。

func AlbumPermissions() -> Int {//相册权限判断器
switch PHPhotoLibrary.authorizationStatus() {

case .notDetermined:// 用户暂未权限认证
    print("PHAuthorizationStatus.NotDetermined")
    // 权限认证
    PHPhotoLibrary.requestAuthorization { (status:PHAuthorizationStatus) -> Void in
        print(status)
    }

    return 0
case .restricted:// APP禁止使用相册权限认证
    print("PHAuthorizationStatus.Restricted")
    return 1
case .denied:// 用户拒绝使用相册
    print("PHAuthorizationStatus.Denied")
    print("请进入 设置 -> 隐私 -> 相册 开启权限")
    return 2
case .authorized: // 用户允许使用相册
    print("PHAuthorizationStatus.Authorized")
    return 3
}}