/
SCPermissionsView.m
122 lines (110 loc) · 5.11 KB
/
SCPermissionsView.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//
// SCPermissionsView.m
// SCCamera
//
// Created by SeacenLiu on 2019/4/11.
// Copyright © 2019 SeacenLiu. All rights reserved.
//
#import "SCPermissionsView.h"
#import <AVFoundation/AVCaptureDevice.h>
#import "UIView+SCCategory.h"
@interface SCPermissionsView ()
@property (nonatomic, strong) UIButton *cameraBtn;
@property (nonatomic, strong) UIButton *microphoneBtn;
@end
@implementation SCPermissionsView
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self setupUI];
[self setupCurStatus];
}
return self;
}
- (void)setupCurStatus {
/*
AVAuthorizationStatusNotDetermined = 0,
AVAuthorizationStatusRestricted = 1,
AVAuthorizationStatusDenied = 2,
AVAuthorizationStatusAuthorized = 3,
*/
AVAuthorizationStatus cameraStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
BOOL cameraGranted = cameraStatus == AVAuthorizationStatusAuthorized;
[self btnChange:self.cameraBtn granted:cameraGranted];
AVAuthorizationStatus microphoneStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
BOOL microphoneGranted = microphoneStatus == AVAuthorizationStatusAuthorized;
[self btnChange:self.microphoneBtn granted:microphoneGranted];
}
- (void)btnChange:(UIButton*)btn granted:(BOOL)granted {
[btn setEnabled:!granted];
// 判断权限都获取了的状态
if (!self.cameraBtn.enabled && !self.microphoneBtn.enabled) {
if ([self.delegate respondsToSelector:@selector(permissionsViewDidHasAllPermissions:)]) {
[self.delegate permissionsViewDidHasAllPermissions:self];
}
}
}
- (void)obtainPermission:(UIButton*)sender {
AVMediaType type = AVMediaTypeVideo;
if (sender == self.microphoneBtn)
type = AVMediaTypeAudio;
switch ([AVCaptureDevice authorizationStatusForMediaType:type]) {
case AVAuthorizationStatusNotDetermined: {
// 手动询问
[AVCaptureDevice requestAccessForMediaType:type completionHandler:^(BOOL granted) {
dispatch_async(dispatch_get_main_queue(), ^{
[self btnChange:sender granted:granted];
});
}];
break;
}
case AVAuthorizationStatusDenied:
// 跳转设置界面
[self openSetting];
break;
default:
break;
}
}
- (void)openSetting {
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
}
- (void)setupUI {
self.backgroundColor = [UIColor blackColor];
[self addSubview:self.cameraBtn];
[self addSubview:self.microphoneBtn];
[_cameraBtn setTranslatesAutoresizingMaskIntoConstraints:NO];
[_microphoneBtn setTranslatesAutoresizingMaskIntoConstraints:NO];
[self addConstraint:[NSLayoutConstraint constraintWithItem:_cameraBtn attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:_cameraBtn attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:0.8 constant:0]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:_microphoneBtn attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:_microphoneBtn attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
}
#pragma mark - lazy
- (UIButton *)cameraBtn {
if (_cameraBtn == nil) {
_cameraBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[_cameraBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[_cameraBtn setTitle:@"允许访问相机" forState:UIControlStateNormal];
[_cameraBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled];
[_cameraBtn setTitle:@"相机访问权限已启用" forState:UIControlStateDisabled];
[_cameraBtn addTarget:self action:@selector(obtainPermission:) forControlEvents:UIControlEventTouchUpInside];
[_cameraBtn sizeToFit];
}
return _cameraBtn;
}
- (UIButton *)microphoneBtn {
if (_microphoneBtn == nil) {
_microphoneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[_microphoneBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[_microphoneBtn setTitle:@"允许访问麦克风" forState:UIControlStateNormal];
[_microphoneBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled];
[_microphoneBtn setTitle:@"麦克风权限已启用" forState:UIControlStateDisabled];
[_microphoneBtn addTarget:self action:@selector(obtainPermission:) forControlEvents:UIControlEventTouchUpInside];
[_microphoneBtn sizeToFit];
}
return _microphoneBtn;
}
@end