我有一个设置视图,用户可以选择打开或关闭“导出到照片库”功能。
当用户首次打开它(而不是第一次拍照时),我希望应用程序要求用户允许访问照片库。
我在许多应用程序中看到了这种行为,但找不到实现的方法。
我有一个设置视图,用户可以选择打开或关闭“导出到照片库”功能。
当用户首次打开它(而不是第一次拍照时),我希望应用程序要求用户允许访问照片库。
我在许多应用程序中看到了这种行为,但找不到实现的方法。
我不确定是否有内置的方法来实现此功能,但一种简单的方法是使用ALAssetsLibrary从照片库中提取一些无意义的信息,当您启用该功能时。然后,您可以简单地将您提取的任何信息设置为null,并提示用户允许访问他们的照片。
例如,下面的代码只是获取相机胶卷中的照片数量,但已足以触发权限提示。
#import <AssetsLibrary/AssetsLibrary.h>
ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init];
[lib enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
NSLog(@"%zd", [group numberOfAssets]);
} failureBlock:^(NSError *error) {
if (error.code == ALAssetsLibraryAccessUserDeniedError) {
NSLog(@"user denied access, code: %zd", error.code);
} else {
NSLog(@"Other error code: %zd", error.code);
}
}];
编辑:刚刚发现这个,以下是如何检查你的应用程序访问相册的授权状态。
ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];
if (status != ALAuthorizationStatusAuthorized) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Attention" message:@"Please give this app permission to access your photo library in your settings app!" delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil, nil];
[alert show];
}
iOS 8自带照片框架,使用方法如下:
Swift 3.0:
PHPhotoLibrary.requestAuthorization { status in
switch status {
case .authorized:
<#your code#>
case .restricted:
<#your code#>
case .denied:
<#your code#>
default:
// place for .notDetermined - in this callback status is already determined so should never get here
break
}
}
Objective-C:
目标-C。[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
switch (status) {
case PHAuthorizationStatusAuthorized:
<#your code#>
break;
case PHAuthorizationStatusRestricted:
<#your code#>
break;
case PHAuthorizationStatusDenied:
<#your code#>
break;
default:
break;
}
}];
来自文档的重要提示:
该方法始终立即返回。如果用户之前已授权或拒绝照片库访问权限,则在调用时执行处理程序块;否则,它会显示一个警报,并仅在用户响应警报后执行块。
dispatch_async(dispatch_get_main_queue(), ^{ -- 这里是与UI相关的代码 -- });
- Alexandre Gimport Photos
:) - judepereirarequestAuthorization
(或任何依赖于照片授权的API)将会崩溃。 - rickster自从iOS 10以来,我们还需要在info.plist
文件中提供照片库使用说明,我在这里进行了描述。然后,只需使用此代码使警报出现每次我们需要:
- (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];
}
}];
}
});
}
此外,有一些常见情况下警报不会出现。为了避免抄袭,我想让你看看这个答案。
Swift:
import AssetsLibrary
var status:ALAuthorizationStatus = ALAssetsLibrary.authorizationStatus()
if status != ALAuthorizationStatus.Authorized{
println("User has not given authorization for the camera roll")
}
#import <AssetsLibrary/AssetsLibrary.h>
//////
ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];
switch (status) {
case ALAuthorizationStatusRestricted:
{
//Tell user access to the photos are restricted
}
break;
case ALAuthorizationStatusDenied:
{
// Tell user access has previously been denied
}
break;
case ALAuthorizationStatusNotDetermined:
case ALAuthorizationStatusAuthorized:
// Try to show image picker
myPicker = [[UIImagePickerController alloc] init];
myPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
myPicker.delegate = self;
[self presentViewController: myPicker animated:YES completion:NULL];
break;
default:
break;
}
iOS 9.2.1,Xcode 7.2.1,启用ARC
'ALAuthorizationStatus'已弃用:自iOS 9.0起首次弃用 - 请改用Photos框架中的PHAuthorizationStatus
请参阅此帖子以获取更新的解决方案:
确定是否设置了对照片库的访问权限 - PHPhotoLibrary(iOS 8)
关键要点:
ALAuthorizationStatus
和PHAuthorizationStatus
。最简单的方法是...
if ([PHPhotoLibrary class])
{
//Use the Photos framework
}
else
{
//Use the Asset Library framework
}
你需要决定要使用哪个媒体集合作为你的源,这取决于你的应用程序将在哪个设备上运行以及它使用的操作系统版本。
如果授权被用户拒绝,您可能希望引导用户到设置页面。