tec*_*man 92 ios ios8 phphotolibrary
使用iOS 8中的新功能,如果您在应用程序中使用相机,它将要求访问相机的权限,然后当您尝试重新拍摄照片时,它会要求获得访问照片库的权限.下次启动应用程序时,我希望检查相机和照片库是否具有访问权限.

对于相机,我检查一下
if ([AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo] == AVAuthorizationStatusDenied)
{
// do something
}
Run Code Online (Sandbox Code Playgroud)
我正在为照片库寻找类似的东西.
Sup*_*off 125
我知道这已经得到了解答,但只是为了扩展@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.
}
Run Code Online (Sandbox Code Playgroud)
别忘了 #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.
}
Run Code Online (Sandbox Code Playgroud)
别忘了 import Photos
Tim*_*Tim 82
检查+[PHPhotoLibrary authorizationStatus]- 如果没有设置,它将返回PHAuthorizationStatusNotDetermined.(然后,您可以+requestAuthorization:在同一个类上请求访问权限.)
Kro*_*dak 41
就像形式一样,Swift 2.X版本:
func checkPhotoLibraryPermission() {
let status = PHPhotoLibrary.authorizationStatus()
switch status {
case .Authorized:
//handle authorized status
case .Denied, .Restricted :
//handle denied status
case .NotDetermined:
// ask for permissions
PHPhotoLibrary.requestAuthorization() { (status) -> Void in
switch status {
case .Authorized:
// as above
case .Denied, .Restricted:
// as above
case .NotDetermined:
// won't happen but still
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
和Swift 3/Swift 4:
func checkPhotoLibraryPermission() {
let status = PHPhotoLibrary.authorizationStatus()
switch status {
case .authorized:
//handle authorized status
case .denied, .restricted :
//handle denied status
case .notDetermined:
// ask for permissions
PHPhotoLibrary.requestAuthorization { status in
switch status {
case .authorized:
// as above
case .denied, .restricted:
// as above
case .notDetermined:
// won't happen but still
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
Jus*_*dow 25
以下是iOS 8及更高版本(不含ALAssetLibrary)的完整指南:
首先,我们必须提供使用说明作为现在它需要通过PHPhotoLibrary.
为此,我们必须打开info.plist文件,找到密钥Privacy - Photo Library Usage Description并为其提供价值.如果key不存在则只需创建它.
这是一个图像,例如:
还要确保key的值Bundle name在info.plist文件中不为空.
现在,当我们有描述时,我们通常可以通过调用requestAuthorization方法请求授权:
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
switch (status) {
case PHAuthorizationStatusAuthorized:
NSLog(@"PHAuthorizationStatusAuthorized");
break;
case PHAuthorizationStatusDenied:
NSLog(@"PHAuthorizationStatusDenied");
break;
case PHAuthorizationStatusNotDetermined:
NSLog(@"PHAuthorizationStatusNotDetermined");
break;
case PHAuthorizationStatusRestricted:
NSLog(@"PHAuthorizationStatusRestricted");
break;
}
}];
Run Code Online (Sandbox Code Playgroud)
注1: requestAuthorization实际上每次通话都不显示警报.它每隔一段时间显示一次,保存用户的答案并每次返回,而不是再次显示警报.但是因为它不是我们需要的,所以这里有一个有用的代码,每次我们需要权限时都会显示警报(重定向到设置):
- (void)requestAuthorizationWithRedirectionToSettings {
dispatch_async(dispatch_get_main_queue(), ^{
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
if (status == PHAuthorizationStatusAuthorized)
{
//We have permission. Do whatever is needed
}
else
{
//No permission. Trying to normally request it
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
if (status != PHAuthorizationStatusAuthorized)
{
//User don't give us permission. Showing alert with redirection to settings
//Getting description string from info.plist file
NSString *accessDescription = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSPhotoLibraryUsageDescription"];
UIAlertController * alertController = [UIAlertController alertControllerWithTitle:accessDescription message:@"To give permissions tap on 'Change Settings' button" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
[alertController addAction:cancelAction];
UIAlertAction *settingsAction = [UIAlertAction actionWithTitle:@"Change Settings" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}];
[alertController addAction:settingsAction];
[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
}
}];
}
});
}
Run Code Online (Sandbox Code Playgroud)
常见问题1:有些用户抱怨应用程序在执行上述info.plist文件更改后未显示警报.
解决方案:为了测试尝试Bundle Identifier从项目文件更改为其他内容,请清理并重建应用程序.如果它开始工作,那么一切都很好,重新命名.
常见问题2:当应用程序获得照片权限,同时按照文档中的承诺运行时,有一些特定情况,即获取结果未更新(并且使用来自这些获取请求的图像的视图仍然相应).
实际上,当我们使用这样的错误代码时会发生这种情况:
- (void)viewDidLoad {
if ([PHPhotoLibrary authorizationStatus] != PHAuthorizationStatusAuthorized)
{
//Reloading some view which needs photos
[self reloadCollectionView];
// ...
} else {
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
if (status == PHAuthorizationStatusAuthorized)
[self reloadCollectionView];
// ...
}];
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,如果用户拒绝授予权限viewDidLoad然后跳转到设置,允许并跳回应用程序,则视图将不会刷新,因为[self reloadCollectionView]并未发送获取请求.
解决方案:我们只需要[self reloadCollectionView]在需要这样的授权之前调用并执行其他获取请求:
- (void)viewDidLoad {
//Reloading some view which needs photos
[self reloadCollectionView];
if ([PHPhotoLibrary authorizationStatus] != PHAuthorizationStatusAuthorized)
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
Sha*_*des 19
我是这样做的:
- (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;
}
}
Run Code Online (Sandbox Code Playgroud)
我根据成功或失败发送我需要做的事情.
iOS 14 及更高版本 Apple 添加了一项新功能,可以限制对照片库的访问。根据您的要求(例如创建自定义照片库),您必须检查用户是否仅授予有限访问权限并希望授予完全访问权限。
为了向后兼容,即使您获得有限的访问权限,不带参数的旧版本也会返回 .authorized。
斯威夫特5:
switch PHPhotoLibrary.authorizationStatus(for: .readWrite) {
case .notDetermined:
// ask for access
case .restricted, .denied:
// sorry
case .authorized:
// we have full access
// new option:
case .limited:
// we only got access to some photos of library
}
Run Code Online (Sandbox Code Playgroud)
有一个代码可以再次调用受限访问屏幕。如果用户仅授予 .limited 访问权限并且您希望用户再次选择图像。
PHPhotoLibrary.shared().presentLimitedLibraryPicker(from: presentVCObj)
Run Code Online (Sandbox Code Playgroud)
在每次重新启动应用程序时,iOS 都会显示警报以通知用户访问受限。如果您想停止该警报PHPhotoLibraryPreventAutomaticLimitedAccessAlert,请在Info.plist 中添加
YES
更新:SWIFT 3 IOS10
注意:在AppDelegate.swift中导入照片,如下所示
// AppDelegate.swift
导入UIKit
导入照片
...
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
photoLibraryAvailabilityCheck()
}
//MARK:- PHOTO LIBRARY ACCESS CHECK
func photoLibraryAvailabilityCheck()
{
if PHPhotoLibrary.authorizationStatus() == PHAuthorizationStatus.authorized
{
}
else
{
PHPhotoLibrary.requestAuthorization(requestAuthorizationHandler)
}
}
func requestAuthorizationHandler(status: PHAuthorizationStatus)
{
if PHPhotoLibrary.authorizationStatus() == PHAuthorizationStatus.authorized
{
}
else
{
alertToEncouragePhotoLibraryAccessWhenApplicationStarts()
}
}
//MARK:- CAMERA & GALLERY NOT ALLOWING ACCESS - ALERT
func alertToEncourageCameraAccessWhenApplicationStarts()
{
//Camera not available - Alert
let internetUnavailableAlertController = UIAlertController (title: "Camera Unavailable", message: "Please check to see if it is disconnected or in use by another application", preferredStyle: .alert)
let settingsAction = UIAlertAction(title: "Settings", style: .destructive) { (_) -> Void in
let settingsUrl = NSURL(string:UIApplicationOpenSettingsURLString)
if let url = settingsUrl {
DispatchQueue.main.async {
UIApplication.shared.open(url as URL, options: [:], completionHandler: nil) //(url as URL)
}
}
}
let cancelAction = UIAlertAction(title: "Okay", style: .default, handler: nil)
internetUnavailableAlertController .addAction(settingsAction)
internetUnavailableAlertController .addAction(cancelAction)
self.window?.rootViewController!.present(internetUnavailableAlertController , animated: true, completion: nil)
}
func alertToEncouragePhotoLibraryAccessWhenApplicationStarts()
{
//Photo Library not available - Alert
let cameraUnavailableAlertController = UIAlertController (title: "Photo Library Unavailable", message: "Please check to see if device settings doesn't allow photo library access", preferredStyle: .alert)
let settingsAction = UIAlertAction(title: "Settings", style: .destructive) { (_) -> Void in
let settingsUrl = NSURL(string:UIApplicationOpenSettingsURLString)
if let url = settingsUrl {
UIApplication.shared.open(url as URL, options: [:], completionHandler: nil)
}
}
let cancelAction = UIAlertAction(title: "Okay", style: .default, handler: nil)
cameraUnavailableAlertController .addAction(settingsAction)
cameraUnavailableAlertController .addAction(cancelAction)
self.window?.rootViewController!.present(cameraUnavailableAlertController , animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)
答案来自Alvin George
使用ALAssetsLibrary应该工作:
ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];
switch (status) {
case ALAuthorizationStatusNotDetermined: {
// not determined
break;
}
case ALAuthorizationStatusRestricted: {
// restricted
break;
}
case ALAuthorizationStatusDenied: {
// denied
break;
}
case ALAuthorizationStatusAuthorized: {
// authorized
break;
}
default: {
break;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
75684 次 |
| 最近记录: |