iOS 系统权限

作者: SunnyLeong | 来源:发表于2017-11-14 09:18 被阅读81次
系统权限问题

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];
}


谢谢!!!

相关文章

  • 检测 iOS 系统网络权限被关闭

    检测 iOS 系统网络权限被关闭 检测 iOS 系统网络权限被关闭

  • 越狱-概述及工具使用

    通过iOS系统安全启动链漏洞,从而禁止掉信任链中负责验证的组件。拿到iOS系统最大权限ROOT权限. iOS系统安...

  • 01_iOS Jailbreak初识

    iOS越狱(iOS Jailbreak)概念 利用iOS系统的漏洞,获取iOS系统的最高权限(Root),解开之前...

  • 越狱环境搭建

    越狱简介 iOS越狱(iOS Jailbreak):利用iOS系统漏洞,获取iOS系统的最高(root)权限,解开...

  • iOS系统权限获取

    iOS系统权限: 相册、相机、麦克风、推送通知、AppleMusic 相册权限状态:ALAuthorization...

  • 获取用户授权,如:定位,通知,录音等。

    获取用户位置权限 iOS获取系统相关权限(iOS 7以上) 先来看看位置的一些权限: 这里就列出我用的定位的代码:...

  • Xcode8文章收集 + iOS10问题收集

    iOS10系统权限 �文章来源 1.麦克风权限:Privacy - Microphone Usage Descri...

  • iOS 系统权限

    iOS 10加强的隐私数据保护,需要使用系统权限必须弹框提示用户,同意才能使用,而且要在plist中添加相应的ke...

  • 获取iOS中的一些权限状态

    查看系统相册权限 在应用中获取相册的权限值, 然后做相应的操作。 iOS8.0以上可以用 查看系统相册权限 在i...

  • 什么是越狱、Cydia

    一、越狱越狱是指通过分析 iOS 系统的代码,找到 iOS 系统的漏洞,绕过系统的安全权限检查,最终获取系统 ro...

网友评论

    本文标题:iOS 系统权限

    本文链接:https://www.haomeiwen.com/subject/gklxvxtx.html