99#import < CoreLocation/CoreLocation.h>
1010#import < AVFoundation/AVFoundation.h>
1111#import " TIoTCoreUtil.h"
12+ #import < Photos/Photos.h>
1213
1314@interface TIoTAuthentationVC ()<UITableViewDelegate, UITableViewDataSource, CBCentralManagerDelegate,CLLocationManagerDelegate>
1415@property (nonatomic , strong ) UITableView *tableView;
@@ -19,6 +20,7 @@ @interface TIoTAuthentationVC ()<UITableViewDelegate, UITableViewDataSource, CBC
1920
2021@property (nonatomic , strong ) CLLocationManager *locationManager;
2122@property (nonatomic , assign ) BOOL locationAvailable; // 地图是否可用
23+ @property (strong , nonatomic ) UIImagePickerController *picker;
2224@end
2325
2426@implementation TIoTAuthentationVC
@@ -101,8 +103,10 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
101103 }else if (indexPath.section == 2 ) {
102104 cell.arrowSwitch .on = [self audioAuthority: AVMediaTypeVideo];
103105 }else if (indexPath.section == 3 ) {
104- cell.arrowSwitch .on = [self audioAuthority: AVMediaTypeAudio ];
106+ cell.arrowSwitch .on = [self authoriedPhotoAlbum ];
105107 }else if (indexPath.section == 4 ) {
108+ cell.arrowSwitch .on = [self audioAuthority: AVMediaTypeAudio];
109+ }else if (indexPath.section == 5 ) {
106110 cell.arrowSwitch .on = self.bluetoothAvailable ;
107111 }
108112
@@ -140,6 +144,39 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
140144 [self jumpSetting ];
141145 }
142146 }else if (indexPath.section == 3 ) {
147+ if ([self getPhoteNotDetermStatus ]) {
148+ BOOL isAuth = [UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypePhotoLibrary];
149+ if (!isAuth) return ;
150+ [PHPhotoLibrary requestAuthorization: ^(PHAuthorizationStatus status) { // 弹出访问权限提示框
151+ if (status == PHAuthorizationStatusAuthorized) {
152+ dispatch_async (dispatch_get_main_queue (),^{
153+ // 同意授权
154+ switchControl.on = YES ;
155+
156+ });
157+ }if (status == PHAuthorizationStatusRestricted || status == PHAuthorizationStatusNotDetermined) {
158+ // 未授权
159+ switchControl.on = NO ;
160+ }if (status == PHAuthorizationStatusDenied) {
161+ dispatch_async (dispatch_get_main_queue (),^{
162+ // 拒绝授权
163+ switchControl.on = NO ;
164+ });
165+ }else {
166+ if (@available (iOS 14 , *)) {
167+ if (status == PHAuthorizationStatusLimited) {
168+ dispatch_async (dispatch_get_main_queue (),^{
169+ // 权限只在 accessLevel 为 PHAccessLevelReadWrite 时生效
170+ switchControl.on = YES ;
171+ });
172+ }
173+ }
174+ }
175+ }];
176+ }else {
177+ [self jumpSetting ];
178+ }
179+ }else if (indexPath.section == 4 ) {
143180 if ([self getMediaNotDetermStatusWithType: AVMediaTypeAudio]) {
144181 [AVCaptureDevice requestAccessForMediaType: AVMediaTypeAudio
145182 completionHandler: ^(BOOL granted) {
@@ -156,7 +193,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
156193 }else {
157194 [self jumpSetting ];
158195 }
159- }else if (indexPath. section == 4 ) {
196+ }else {
160197 if (self.centralManager .state == CBManagerStateUnauthorized) {
161198 // 判断蓝牙是否开启
162199 self.centralManager = [[CBCentralManager alloc ] initWithDelegate: self queue: nil options: nil ];
@@ -210,6 +247,7 @@ - (NSMutableArray *)dataArr{
210247 @[@{@" title" :NSLocalizedString(@" authentation_privacy_conte1" , @" 通知推送权限" ),@" value" :@" " ,@" vc" :@" " ,@" haveArrow" :@" 2" }],
211248 @[@{@" title" :NSLocalizedString(@" authentation_privacy_conte2" , @" 位置信息" ),@" value" :@" " ,@" vc" :@" " ,@" haveArrow" :@" 2" }],
212249 @[@{@" title" :NSLocalizedString(@" authentation_privacy_conte3" , @" 摄像头权限" ),@" value" :@" " ,@" vc" :@" " ,@" haveArrow" :@" 2" }],
250+ @[@{@" title" :NSLocalizedString(@" authentation_privacy_conte6" , @" 相册权限" ),@" value" :@" " ,@" vc" :@" " ,@" haveArrow" :@" 2" }],
213251 @[@{@" title" :NSLocalizedString(@" authentation_privacy_conte5" , @" 麦克风权限" ),@" value" :@" " ,@" vc" :@" " ,@" haveArrow" :@" 2" }],
214252 @[@{@" title" :NSLocalizedString(@" authentation_privacy_conte4" , @" 蓝牙权限" ),@" value" :@" " ,@" vc" :@" " ,@" haveArrow" :@" 2" }],
215253 ]];
@@ -228,6 +266,15 @@ - (BOOL)getMediaNotDetermStatusWithType:(AVMediaType)mediaType {
228266 }
229267}
230268
269+ - (BOOL )getPhoteNotDetermStatus {
270+ PHAuthorizationStatus status= [PHPhotoLibrary authorizationStatus ];
271+ if (status == PHAuthorizationStatusNotDetermined) {
272+ return YES ;
273+ }else {
274+ return NO ;
275+ }
276+ }
277+
231278- (void )jumpSetting {
232279 NSURL *url = [NSURL URLWithString: UIApplicationOpenSettingsURLString];
233280 if ([[UIApplication sharedApplication ] canOpenURL: url]){
@@ -297,6 +344,27 @@ - (BOOL)locationAuthority {
297344 return YES ;
298345}
299346
347+ - (BOOL )authoriedPhotoAlbum {
348+ BOOL isAccess = NO ;
349+ PHAuthorizationStatus status= [PHPhotoLibrary authorizationStatus ];
350+ if (status == PHAuthorizationStatusNotDetermined) {
351+ isAccess = NO ;
352+ }else if (status == PHAuthorizationStatusRestricted || status == PHAuthorizationStatusDenied) {
353+ isAccess = NO ;
354+ }else if (status == PHAuthorizationStatusAuthorized) {
355+ isAccess = YES ;
356+ }else {
357+ if (@available (iOS 14 , *)) {
358+ if (status == PHAuthorizationStatusLimited) {
359+ isAccess = YES ;
360+ }
361+ }else {
362+ isAccess = NO ;
363+ }
364+ }
365+ return isAccess;
366+ }
367+
300368- (BOOL )audioAuthority : (AVMediaType)type {
301369// return [TIoTCoreUtil requestMediaAuthorization:type];
302370 return [TIoTCoreUtil userAccessMediaAuthorization: type];
@@ -368,4 +436,14 @@ - (void)locationManagerDidChangeAuthorization:(CLLocationManager *)manager API_A
368436 }
369437 [self .tableView reloadData ];
370438}
439+
440+ #pragma mark - lazyloading
441+ - (UIImagePickerController *)picker
442+ {
443+ if (!_picker) {
444+ _picker = [[UIImagePickerController alloc ]init];
445+ _picker.allowsEditing = NO ;
446+ }
447+ return _picker;
448+ }
371449@end
0 commit comments