通过iOS 8的新功能,如果你在应用程序中使用相机,它会要求获得访问相机的许可,然后当你试图重新拍摄照片时,它会要求获得访问图片库的许可。下次启动应用程序时,我希望检查相机和图片库是否有访问权限。
对于相机,我通过以下方式检查
if ([AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo] == AVAuthorizationStatusDenied)
{
// do something
}
我正在为图片库寻找类似的东西。
发布于 2014-10-28 03:36:44
检查+[PHPhotoLibrary authorizationStatus]
-如果未设置,它将返回PHAuthorizationStatusNotDetermined
。(然后可以在同一个类上使用+requestAuthorization:
请求访问权限。)
发布于 2015-10-07 17:46:37
我知道这个问题已经回答了,但为了扩展@Tim answer,这里是您需要的代码(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
发布于 2015-08-26 04:00:23
我是这样做的:
- (void)requestPermissions:(GalleryPermissions)block
{
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
switch (status)
{
case PHAuthorizationStatusAuthorized:
block(YES);
break;
case PHAuthorizationStatusNotDetermined:
{
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus authorizationStatus)
{
if (authorizationStatus == PHAuthorizationStatusAuthorized)
{
block(YES);
}
else
{
block(NO);
}
}];
break;
}
default:
block(NO);
break;
}
}
我根据成功或失败发送我需要做的代码块。
https://stackoverflow.com/questions/26595343
复制相似问题