iOS 10加强的隐私数据保护,需要使用系统权限必须弹框提示用户,同意才能使用,而且要在plist中添加相应的key。
10之前只需要获取位置时配置,现在更严格了,比如需要调用相册访问权限,也需要在Info.plist中配置privacy。好在这些key的名字在Xcode 8中已经有了自动补全。添加一个属性,输入Privacy后就会出现自动提示:
后面填的string会在弹出用户允许时展示在描述里。如果描述空着提交AppStore时会拒绝。
用户在点击允许后一切正常,但是点击不允许之后就需要对权限的状态进行判断,并作出相应的处理。
或者你直接粘贴需要的权限:
<key>NSPhotoLibraryUsageDescription</key>
<string>我们需要您的相册权限</string>
<key>NSMicrophoneUsageDescription</key>
<string>我们需要您的麦克风权限</string>
<key>NSCameraUsageDescription</key>
<string>我们需要您的相机权限</string>
- ViewController.h文件
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
- ViewController.m文件
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <Photos/Photos.h>//相册
@interface ViewController ()
@property (nonatomic, strong) UIButton * audioType;
@property (nonatomic, strong) UIButton * videoType;
@property (nonatomic, strong) UIButton * photoLibraryType;
@property (nonatomic, assign) NSString* audioStatus;
@property (nonatomic, assign) NSString* videoStatus;
@property (nonatomic, assign) NSString* photoLibraryStatus;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.audioType = [[UIButton alloc] initWithFrame:CGRectMake(10,100 , 350, 50)];
[self.audioType setBackgroundColor:[UIColor redColor]];
[self.view addSubview:self.audioType ];
[self.audioType addTarget:self action:@selector(audioAuthAction) forControlEvents:UIControlEventTouchUpInside];
self.videoType = [[UIButton alloc] initWithFrame:CGRectMake(10, 180, 350, 50)];
[self.videoType setBackgroundColor:[UIColor greenColor]];
[self.view addSubview:self.videoType];
[self.videoType addTarget:self action:@selector(videoAuthAction) forControlEvents:UIControlEventTouchUpInside];
self.photoLibraryType = [[UIButton alloc] initWithFrame:CGRectMake(10, 250, 350, 50)];
[self.photoLibraryType setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:self.photoLibraryType];
[self.photoLibraryType addTarget:self action:@selector(phontLibraryAction) forControlEvents:UIControlEventTouchUpInside];
[self checkAudioStatus];
[self checkVideoStatus];
[self checkPhotoStauts];
}
//授权麦克风
- (void)audioAuthAction
{
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
NSLog(@"%@",granted ? @"麦克风准许":@"麦克风不准许");
[self checkAudioStatus];
}];
}
//授权相机
- (void)videoAuthAction
{
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
NSLog(@"%@",granted ? @"相机准许":@"相机不准许");
[self checkVideoStatus];
}];
}
//授权照片
- (void)phontLibraryAction{
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
[self checkPhotoStauts];
}];
}
//检查麦克风权限
- (void) checkAudioStatus{
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
switch (authStatus) {
case AVAuthorizationStatusNotDetermined:
//没有询问是否开启麦克风
self.audioStatus = @"AVAuthorizationStatusNotDetermined";
break;
case AVAuthorizationStatusRestricted:
//未授权,家长限制
self.audioStatus = @"AVAuthorizationStatusRestricted";
break;
case AVAuthorizationStatusDenied:
//玩家未授权
self.audioStatus = @"AVAuthorizationStatusDenied";
break;
case AVAuthorizationStatusAuthorized:
//玩家授权
self.audioStatus = @"AVAuthorizationStatusAuthorized";
break;
default:
break;
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.audioType setTitle:self.audioStatus forState:UIControlStateNormal];
});
}
//检查相机权限
- (void) checkVideoStatus
{
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
switch (authStatus) {
case AVAuthorizationStatusNotDetermined:
//没有询问是否开启相机
self.videoStatus = @"AVAuthorizationStatusNotDetermined";
break;
case AVAuthorizationStatusRestricted:
//未授权,家长限制
self.videoStatus = @"AVAuthorizationStatusRestricted";
break;
case AVAuthorizationStatusDenied:
//未授权
self.videoStatus = @"AVAuthorizationStatusDenied";
break;
case AVAuthorizationStatusAuthorized:
//玩家授权
self.videoStatus = @"AVAuthorizationStatusAuthorized";
break;
default:
break;
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.videoType setTitle:self.videoStatus forState:UIControlStateNormal];
});
}
//检查照片权限
- (void) checkPhotoStauts{
PHAuthorizationStatus photoAuthorStatus = [PHPhotoLibrary authorizationStatus];
switch (photoAuthorStatus) {
case PHAuthorizationStatusAuthorized:
self.photoLibraryStatus = @"PHAuthorizationStatusAuthorized";
break;
case PHAuthorizationStatusDenied:
self.photoLibraryStatus = @"PHAuthorizationStatusDenied";
break;
case PHAuthorizationStatusNotDetermined:
self.photoLibraryStatus = @"PHAuthorizationStatusNotDetermined";
break;
case PHAuthorizationStatusRestricted:
self.photoLibraryStatus = @"PHAuthorizationStatusRestricted";
break;
default:
break;
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.photoLibraryType setTitle:self.photoLibraryStatus forState:UIControlStateNormal];
});
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
如果你拒绝了某个权限,以麦克风为例,下次用到此功能需要给予用户提示:
//提示用户进行麦克风使用授权
- (void)showSetAlertView {
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"麦克风权限未开启" message:@"麦克风权限未开启,请进入系统【设置】>【隐私】>【麦克风】中打开开关,开启麦克风功能" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
UIAlertAction *setAction = [UIAlertAction actionWithTitle:@"去设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//跳入当前App设置界面
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}];
[alertVC addAction:cancelAction];
[alertVC addAction:setAction];
[self presentViewController:alertVC animated:YES completion:nil];
}
谢谢!!!