gpt4 book ai didi

ios - 确定是否设置了对照片库的访问 - PHPhotoLibrary

转载 作者:IT王子 更新时间:2023-10-29 07:30:00 27 4
gpt4 key购买 nike

随着 iOS 8 中的新功能,如果您在应用程序中使用相机,它会请求访问相机的权限,然后当您尝试重新拍摄照片时,它会请求访问照片库的权限。下次我启动应用程序时,我想检查相机和照片库是否有访问权限。

enter image description here

对于相机,我通过检查

if ([AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo] == AVAuthorizationStatusDenied)
{
// do something
}

我正在为照片库寻找类似的东西。

最佳答案

我知道这已经得到回答,但只是为了扩展@Tim 的回答,这里是您需要的代码(iOS 8 及更高版本):

PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];

if (status == PHAuthorizationStatusAuthorized) {
// Access has been granted.
}

else if (status == PHAuthorizationStatusDenied) {
// Access has been denied.
}

else if (status == PHAuthorizationStatusNotDetermined) {

// Access has not been determined.
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {

if (status == PHAuthorizationStatusAuthorized) {
// Access has been granted.
}

else {
// Access has been denied.
}
}];
}

else if (status == PHAuthorizationStatusRestricted) {
// Restricted access - normally won't happen.
}

别忘了 #import <Photos/Photos.h>

如果您使用的是 Swift 3.0 或更高版本,则可以使用以下代码:

// Get the current authorization state.
let status = PHPhotoLibrary.authorizationStatus()

if (status == PHAuthorizationStatus.authorized) {
// Access has been granted.
}

else if (status == PHAuthorizationStatus.denied) {
// Access has been denied.
}

else if (status == PHAuthorizationStatus.notDetermined) {

// Access has not been determined.
PHPhotoLibrary.requestAuthorization({ (newStatus) in

if (newStatus == PHAuthorizationStatus.authorized) {

}

else {

}
})
}

else if (status == PHAuthorizationStatus.restricted) {
// Restricted access - normally won't happen.
}

别忘了 import Photos

关于ios - 确定是否设置了对照片库的访问 - PHPhotoLibrary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26595343/

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