6

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

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

xcode 提醒我

'使用未解析的标识符'ALAssetsLibrary'</p>

xcode 截图

当我尝试在 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”

4

1 回答 1

12

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

“PHPhotoLibrary”以下列方式正确使用。

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
}}
于 2017-09-28T01:07:41.050 回答