In iOS11, you can not get permission through the "ALAssetsLibrary" or "PHPhotoLibrary" command
Asked Answered
H

1

6

When I try to determine whether the user has permission to open the album, xcode told me that this does not work.

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

xcode remind me

‘Use of unresolved identifier 'ALAssetsLibrary’

xcode screenshot

when i try use 'PHPhotoLibrary' on AppDelegate

    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 also told me

Use of unresolved identifier 'PHPhotoLibrary'

Hartmann answered 28/9, 2017 at 0:33 Comment(2)
It's deprecated, use PHPhotoLibrary instead.Lafollette
@WeiJay thank you,i use your method to solve the current problem!Hartmann
H
12

Ok,I have realized what is the answer, add the “import Photos” at the top of the swift file after everything is normal

"PHPhotoLibrary" is used correctly in the following way.

func AlbumPermissions() -> Int { // Photos Permissions
switch PHPhotoLibrary.authorizationStatus() {
    
case .notDetermined:// User has not permitted the permission
    print("PHAuthorizationStatus.NotDetermined")
    
    PHPhotoLibrary.requestAuthorization { (status:PHAuthorizationStatus) -> Void in
        print(status)
    }
    
    return 0
case .restricted:// APP reject the permission of Photos
    print("PHAuthorizationStatus.Restricted")
    return 1
case .denied:// Users denied the Photos permission
    print("PHAuthorizationStatus.Denied")
    print("Please go to Setting to turn on the Photos permission.")
    return 2
case .authorized: // User permit the Photos permission.
    print("PHAuthorizationStatus.Authorized")
    return 3
}}
Hartmann answered 28/9, 2017 at 1:7 Comment(2)
BTW, you can just return the PHPhotoLibrary.authorizationStatus() for your function. (if needed)Lafollette
The import Photos saved my day.Yevetteyew

© 2022 - 2024 — McMap. All rights reserved.