ios 请求访问相机胶卷的权限
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13572220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ask permission to access Camera Roll
提问by Titouan de Bailleul
I have a settings view where the user can choose switch on or off the feature 'Export to Camera Roll'
我有一个设置视图,用户可以在其中选择打开或关闭“导出到相机胶卷”功能
When the user switches it on for the first time (and not when he takes the first picture), I would like the app to ask him the permission to access the camera roll.
当用户第一次打开它时(而不是在他拍摄第一张照片时),我希望应用程序询问他访问相机胶卷的权限。
I've seen behavior in many app but can't find the way to do it.
我在许多应用程序中看到过行为,但找不到方法。
回答by Mick MacCallum
I'm not sure if there is some build in method for this, but an easy way would be to use ALAssetsLibrary to pull some meaningless bit of information from the photo library when you turn the feature on. Then you can simply nullify what ever info you pulled, and you will have prompted the user for access to their photos.
我不确定是否有一些内置方法,但一个简单的方法是在打开该功能时使用 ALAssetsLibrary 从照片库中提取一些无意义的信息。然后您可以简单地取消您提取的任何信息,您将提示用户访问他们的照片。
The following code for example does nothing more than get the number of photos in the camera roll, but will be enough to trigger the permission prompt.
例如,下面的代码只是获取相机胶卷中的照片数量,但足以触发权限提示。
#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);
}
}];
EDIT:Just stumbled across this, below is how you can check the authorization status of your applications access to photo albums.
编辑:刚刚偶然发现了这个,下面是如何检查您的应用程序访问相册的授权状态。
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];
}
回答by Tomasz B?k
Since iOS 8 with Photos frameworkuse:
由于带有照片框架的iOS 8使用:
Swift 3.0:
斯威夫特 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;
}
}];
Important note from documentation:
文档中的重要说明:
This method always returns immediately. If the user has previously granted or denied photo library access permission, it executes the handler block when called; otherwise, it displays an alert and executes the block only after the user has responded to the alert.
此方法总是立即返回。如果用户先前授予或拒绝了照片库访问权限,则在调用时执行处理程序块;否则,它会显示警报并仅在用户响应警报后才执行该块。
回答by Just Shadow
Since iOS 10 we also need to provide photo library usage description in info.plist
file which I described there. And then just use this code to make alert appear everytime we need:
从 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];
}
}];
}
});
}
Also there are some common cases when alert doesn't appear. To avoid copying I would like you to take a look at this answer.
还有一些常见的情况是没有出现警报。为避免复制,我希望您看看这个答案。
回答by TimoRozendal
The first time the user tries to write to camera roll on ios 6 he/she is automatically asked for permission. You don't have to add extra code (before that the authorisationstatus is ALAuthorizationStatusNotDetermined ).
用户第一次尝试在 ios 6 上写入相机胶卷时,会自动请求他/她的许可。您不必添加额外的代码(在此之前,授权状态是 ALAuthorizationStatusNotDetermined )。
If the user denies the first time you cannot ask again (as far as I know). The user has to manually change that app specific setting in the settings->privacy-> photos section.
如果用户第一次拒绝你就不能再问了(据我所知)。用户必须在设置-> 隐私-> 照片部分手动更改该应用程序特定设置。
There is one other option and that is that it the user cannot give permission due other restrictions like parental control, in that case the status is ALAuthorizationStatusRestricted
还有另一种选择,即由于其他限制(例如家长控制),用户无法授予权限,在这种情况下,状态为 ALAuthorizationStatusRestricted
回答by Esqarrouth
Swift:
迅速:
import AssetsLibrary
var status:ALAuthorizationStatus = ALAssetsLibrary.authorizationStatus()
if status != ALAuthorizationStatus.Authorized{
println("User has not given authorization for the camera roll")
}
回答by Dan Atherton
#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;
}
回答by serge-k
iOS 9.2.1, Xcode 7.2.1, ARC enabled
iOS 9.2.1、Xcode 7.2.1、ARC 已启用
'ALAuthorizationStatus' is deprecated: first deprecated in iOS 9.0 - Use PHAuthorizationStatus in the Photos framework instead
'ALAuthorizationStatus' 已弃用:首先在 iOS 9.0 中弃用 - 在照片框架中使用 PHAuthorizationStatus
Please see this post for an updated solution:
请参阅此帖子以获取更新的解决方案:
Determine if the access to photo library is set or not - PHPhotoLibrary (iOS 8)
确定是否设置了对照片库的访问 - PHPhotoLibrary (iOS 8)
Key notes:
要点:
- Most likely you are designing for iOS7.0+ as of todays date, because of this fact you will need to handle both
ALAuthorizationStatus
andPHAuthorizationStatus
.
- 截至今天,您很可能正在为 iOS7.0+ 进行设计,因此您需要同时处理
ALAuthorizationStatus
和PHAuthorizationStatus
.
The easiest is to do...
最简单的就是做...
if ([PHPhotoLibrary class])
{
//Use the Photos framework
}
else
{
//Use the Asset Library framework
}
You will need to decide which media collection you want to use as your source, this is dictated by the device that your app. will run on and which version of OS it is using.
You might want to direct the user to settings if the authorization is denied by user.
您需要决定要使用哪个媒体集作为您的来源,这取决于您的应用程序的设备。将在哪个版本的操作系统上运行。
如果用户拒绝授权,您可能希望将用户定向到设置。