gpt4 book ai didi

ios - iOS11下无法通过 "ALAssetsLibrary"或 "PHPhotoLibrary"命令获取权限

转载 作者:可可西里 更新时间:2023-11-01 05:07:28 25 4
gpt4 key购买 nike

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

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

xcode 提醒我

‘Use of unresolved identifier '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 也告诉我

Use of unresolved identifier 'PHPhotoLibrary'

最佳答案

好的,我已经意识到答案是什么了,一切正常后在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
}}

关于ios - iOS11下无法通过 "ALAssetsLibrary"或 "PHPhotoLibrary"命令获取权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46459143/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com