1010#import < AVFoundation/AVFoundation.h>
1111#import " TIoTCoreUtil.h"
1212
13- @interface TIoTAuthentationVC ()<UITableViewDelegate, UITableViewDataSource, CBCentralManagerDelegate>
13+ @interface TIoTAuthentationVC ()<UITableViewDelegate, UITableViewDataSource, CBCentralManagerDelegate,CLLocationManagerDelegate >
1414@property (nonatomic , strong ) UITableView *tableView;
1515@property (nonatomic , copy ) NSMutableArray *dataArr;
1616@property (nonatomic , strong ) CBCentralManager *centralManager; // 判断蓝牙是否开启
1717// / 蓝牙是否可用
1818@property (nonatomic , assign ) BOOL bluetoothAvailable;
19+
20+ @property (nonatomic , strong ) CLLocationManager *locationManager;
21+ @property (nonatomic , assign ) BOOL locationAvailable; // 地图是否可用
1922@end
2023
2124@implementation TIoTAuthentationVC
@@ -33,7 +36,17 @@ - (void)viewDidLoad {
3336 [[NSNotificationCenter defaultCenter ] addObserver: self selector: @selector (applicationwillenterforegound ) name: UIApplicationWillEnterForegroundNotification object: nil ];
3437
3538 // 判断蓝牙是否开启
36- self.centralManager = [[CBCentralManager alloc ] initWithDelegate: self queue: nil options: nil ];
39+ if ([[TIoTCoreUserManage shared ].isChangeBluetoothAuth isEqualToString: @" 1" ]) {
40+ self.centralManager = [[CBCentralManager alloc ] initWithDelegate: self queue: nil options: nil ];
41+ self.bluetoothAvailable = YES ;
42+ }else {
43+ if (![NSString isNullOrNilWithObject: [TIoTCoreUserManage shared ].isChangeBluetoothAuth]) {
44+ self.centralManager = [[CBCentralManager alloc ] initWithDelegate: self queue: nil options: nil ];
45+ }else {
46+ self.bluetoothAvailable = NO ;
47+ }
48+
49+ }
3750}
3851
3952- (void )applicationwillenterforegound
@@ -93,13 +106,75 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
93106 cell.arrowSwitch .on = self.bluetoothAvailable ;
94107 }
95108
96- cell.authSwitch = ^(BOOL open) {
97-
98- NSURL *url = [NSURL URLWithString: UIApplicationOpenSettingsURLString];
99- if ([[UIApplication sharedApplication ] canOpenURL: url]){
100- [[UIApplication sharedApplication ] openURL: url options: @{} completionHandler: nil ];
101- }
109+ cell.authSwitch = ^(BOOL open,UISwitch *switchControl) {
102110
111+ if (indexPath.section == 0 ) {
112+ [self jumpSetting ];
113+
114+ }else if (indexPath.section == 1 ) {
115+ CLAuthorizationStatus CLstatus = [CLLocationManager authorizationStatus ];
116+ if (CLstatus == kCLAuthorizationStatusNotDetermined ) {
117+ self.locationManager = [[CLLocationManager alloc ] init ];
118+ self.locationManager .delegate = self;
119+ }else {
120+ [self jumpSetting ];
121+ }
122+
123+ }else if (indexPath.section == 2 ) {
124+ if ([self getMediaNotDetermStatusWithType: AVMediaTypeVideo]) {
125+
126+ [AVCaptureDevice requestAccessForMediaType: AVMediaTypeVideo
127+ completionHandler: ^(BOOL granted) {
128+ dispatch_async (dispatch_get_main_queue (), ^{
129+ if (granted) {
130+ // 同意授权
131+ switchControl.on = YES ;
132+ } else {
133+ // 拒绝授权
134+ switchControl.on = NO ;
135+ }
136+ });
137+ }];
138+
139+ }else {
140+ [self jumpSetting ];
141+ }
142+ }else if (indexPath.section == 3 ) {
143+ if ([self getMediaNotDetermStatusWithType: AVMediaTypeAudio]) {
144+ [AVCaptureDevice requestAccessForMediaType: AVMediaTypeAudio
145+ completionHandler: ^(BOOL granted) {
146+ dispatch_async (dispatch_get_main_queue (), ^{
147+ if (granted) {
148+ // 同意授权
149+ switchControl.on = YES ;
150+ } else {
151+ // 拒绝授权
152+ switchControl.on = NO ;
153+ }
154+ });
155+ }];
156+ }else {
157+ [self jumpSetting ];
158+ }
159+ }else if (indexPath.section == 4 ) {
160+ if (self.centralManager .state == CBManagerStateUnauthorized) {
161+ // 判断蓝牙是否开启
162+ self.centralManager = [[CBCentralManager alloc ] initWithDelegate: self queue: nil options: nil ];
163+ if (![NSString isNullOrNilWithObject: [TIoTCoreUserManage shared ].isChangeBluetoothAuth]) {
164+ [self jumpSetting ];
165+ }
166+ }else {
167+
168+ if ([NSString isNullOrNilWithObject: [TIoTCoreUserManage shared ].isChangeBluetoothAuth]) {
169+ self.centralManager = [[CBCentralManager alloc ] initWithDelegate: self queue: nil options: nil ];
170+ }else {
171+ if ([[TIoTCoreUserManage shared ].isChangeBluetoothAuth isEqualToString: @" 0" ]) {
172+ self.centralManager = [[CBCentralManager alloc ] initWithDelegate: self queue: nil options: nil ];
173+ }
174+ [self jumpSetting ];
175+ }
176+ }
177+ }
103178 };
104179 return cell;
105180}
@@ -143,6 +218,22 @@ - (NSMutableArray *)dataArr{
143218 return _dataArr;
144219}
145220
221+ // 判断是否麦克风和摄像头请求授权
222+ - (BOOL )getMediaNotDetermStatusWithType : (AVMediaType)mediaType {
223+ AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType: mediaType];
224+ if (authStatus == AVAuthorizationStatusNotDetermined) {
225+ return YES ;
226+ }else {
227+ return NO ;
228+ }
229+ }
230+
231+ - (void )jumpSetting {
232+ NSURL *url = [NSURL URLWithString: UIApplicationOpenSettingsURLString];
233+ if ([[UIApplication sharedApplication ] canOpenURL: url]){
234+ [[UIApplication sharedApplication ] openURL: url options: @{} completionHandler: nil ];
235+ }
236+ }
146237
147238#pragma mark - 是否开启APP推送
148239/* *是否开启推送*/
@@ -196,7 +287,7 @@ - (BOOL)locationAuthority {
196287 if (isLocation) {
197288
198289 CLAuthorizationStatus CLstatus = [CLLocationManager authorizationStatus ];
199- if (CLstatus == kCLAuthorizationStatusDenied || CLstatus == kCLAuthorizationStatusDenied ) {
290+ if (CLstatus == kCLAuthorizationStatusDenied || CLstatus == kCLAuthorizationStatusDenied || CLstatus == kCLAuthorizationStatusNotDetermined ) {
200291 return NO ;
201292 }
202293
@@ -207,25 +298,30 @@ - (BOOL)locationAuthority {
207298}
208299
209300- (BOOL )audioAuthority : (AVMediaType)type {
210- return [TIoTCoreUtil requestMediaAuthorization: type];
301+ // return [TIoTCoreUtil requestMediaAuthorization:type];
302+ return [TIoTCoreUtil userAccessMediaAuthorization: type];
211303}
212304
213305#pragma mark - 判断蓝牙是否开启代理
214306- (void )centralManagerDidUpdateState : (CBCentralManager *)central {
215307 switch (central.state ) {
216308 case CBManagerStatePoweredOn:
309+ [TIoTCoreUserManage shared ].isChangeBluetoothAuth = @" 1" ;
217310 self.bluetoothAvailable = true ; break ; // NSLog(@"蓝牙开启且可用");
218311 case CBManagerStateUnknown:
312+ [TIoTCoreUserManage shared ].isChangeBluetoothAuth = @" 0" ;
219313 self.bluetoothAvailable = false ; break ; // NSLog(@"手机没有识别到蓝牙,请检查手机。");
220314 case CBManagerStateResetting:
315+ [TIoTCoreUserManage shared ].isChangeBluetoothAuth = @" 1" ;
221316 self.bluetoothAvailable = false ; break ; // NSLog(@"手机蓝牙已断开连接,重置中。");
222317 case CBManagerStateUnsupported:
223318 self.bluetoothAvailable = false ; break ; // NSLog(@"手机不支持蓝牙功能,请更换手机。");
224319 case CBManagerStatePoweredOff:
225-
226320 [self customAlertOpenBluetooth ];
321+ [TIoTCoreUserManage shared ].isChangeBluetoothAuth = @" 0" ;
227322 self.bluetoothAvailable = false ; break ; // NSLog(@"手机蓝牙功能关闭,请前往设置打开蓝牙及控制中心打开蓝牙。");
228323 case CBManagerStateUnauthorized:
324+ [TIoTCoreUserManage shared ].isChangeBluetoothAuth = @" 0" ;
229325 self.bluetoothAvailable = false ; break ; // NSLog(@"手机蓝牙功能没有权限,请前往设置。");
230326 default : break ;
231327 }
@@ -250,10 +346,26 @@ - (void)customAlertOpenBluetooth {
250346 [alertC addAction: alertCancel];
251347
252348 UIAlertAction *alertConfirm = [UIAlertAction actionWithTitle: NSLocalizedString(@" confirm" , @" 确定" ) style: (UIAlertActionStyleDefault) handler: ^(UIAlertAction * _Nonnull action) {
349+ [self jumpSetting ];
253350 }];
254351 [alertC addAction: alertConfirm];
255352
256353 [self presentViewController: alertC animated: YES completion: nil ];
257354}
258355
356+ #pragma mark CLLocationManagerDelegate
357+
358+ - (void )locationManagerDidChangeAuthorization : (CLLocationManager *)manager API_AVAILABLE(ios(14.0 ), macos(11.0 ), watchos(7.0 ), tvos(14.0 )) {
359+ CLAuthorizationStatus status = [manager authorizationStatus ];
360+ if (status == kCLAuthorizationStatusAuthorizedWhenInUse || status == kCLAuthorizationStatusAuthorizedAlways ) {
361+ self.locationAvailable = YES ;
362+ }else if (status == kCLAuthorizationStatusNotDetermined ) {
363+ self.locationAvailable = NO ;
364+ [manager requestWhenInUseAuthorization ];
365+ }else {
366+ // 提示语弹框
367+ self.locationAvailable = NO ;
368+ }
369+ [self .tableView reloadData ];
370+ }
259371@end
0 commit comments