Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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'

like image 642
Elecoxy Avatar asked Sep 28 '17 00:09

Elecoxy


1 Answers

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
}}
like image 84
Elecoxy Avatar answered Sep 28 '22 07:09

Elecoxy