阐述
从前几年的棱镜门到最近的Apple与FBI关于解锁iPhone的争论,人们开始重视个人隐私地保护。Apple在用户保护隐私这方面做了很多工作,因此受到了广大用户的好评,但变相得增加对iOS开发者的要求(熟练地掌握设备权限)。下面我们开始探讨一下iOS设备中某些权限。(注:iPad、iTouch不含有某些功能)
总括
本文探讨方面包括以下权限:
具体
1. 联网权限
依赖库
@import CoreTelephony; //联网
获取状态
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
| - (void)getTheCurrentNetEnvironment { CTCellularData *cellularData = [[CTCellularData alloc]init]; cellularData.cellularDataRestrictionDidUpdateNotifier = ^(CTCellularDataRestrictedState state){ switch (state) { case kCTCellularDataRestricted: NSLog(@"Update Restricrted"); break; case kCTCellularDataNotRestricted: NSLog(@"Update Not Restricted"); break; case kCTCellularDataRestrictedStateUnknown: NSLog(@"Update Unknown"); break; default: break; }; };
CTCellularDataRestrictedState state = cellularData.restrictedState; switch (state) { case kCTCellularDataRestricted: NSLog(@"Current Restricrted"); break; case kCTCellularDataNotRestricted: NSLog(@"Current Not Restricted"); break; case kCTCellularDataRestrictedStateUnknown: NSLog(@"Current Unknown"); break; default: break; } }
|
2. 相册权限
依赖库
@import AssetsLibrary; // 相册 iOS 6-9
@import Photos; // 相册 iOS 8+
获取状态
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
| - (void)getTheCurrentPhoteAlbumEnvironmentOld { ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus]; switch (status) { case ALAuthorizationStatusAuthorized: NSLog(@"Authorized"); break; case ALAuthorizationStatusDenied: NSLog(@"Denied"); break; case ALAuthorizationStatusNotDetermined: NSLog(@"not Determined"); break; case ALAuthorizationStatusRestricted: NSLog(@"Restricted"); break; default: break; } }
- (void)getTheCurrentPhoteAlbumEnvironment { PHAuthorizationStatus photoAuthorStatus = [PHPhotoLibrary authorizationStatus]; switch (photoAuthorStatus) { case PHAuthorizationStatusAuthorized: NSLog(@"Authorized"); break; case PHAuthorizationStatusDenied: NSLog(@"Denied"); break; case PHAuthorizationStatusNotDetermined: NSLog(@"not Determined"); break; case PHAuthorizationStatusRestricted: NSLog(@"Restricted"); break; default: break; } }
|
设置权限
1 2 3 4 5 6 7 8 9 10
| - (void)setTheCurrentPhoteAlbumEnvironment { [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) { if (status == PHAuthorizationStatusAuthorized) { NSLog(@"Authorized"); }else{ NSLog(@"Denied or Restricted"); } }]; }
|
3. 相机、麦克风权限
依赖库
@import AVFoundation; // 相机和麦克风权限
获取状态
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| - (void)getTheCurrentCameraEnvironment { AVAuthorizationStatus AVstatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
switch (AVstatus) { case AVAuthorizationStatusAuthorized: NSLog(@"Authorized"); break; case AVAuthorizationStatusDenied: NSLog(@"Denied"); break; case AVAuthorizationStatusNotDetermined: NSLog(@"not Determined"); break; case AVAuthorizationStatusRestricted: NSLog(@"Restricted"); break; default: break; } }
|
设置权限
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| - (void)setTheCurrentCameraEnvironment { [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) { if (granted) { NSLog(@"Authorized"); }else{ NSLog(@"Denied or Restricted"); } }];
}
|
4. 定位权限
依赖库
@import CoreLocation; // 定位权限
获取状态
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
| - (void)getTheCurrentLocationEnvironment { BOOL isLocation = [CLLocationManager locationServicesEnabled]; if (!isLocation) { NSLog(@"not turn on the location"); } CLAuthorizationStatus CLstatus = [CLLocationManager authorizationStatus]; switch (CLstatus) { case kCLAuthorizationStatusAuthorizedAlways: NSLog(@"Always Authorized"); break; case kCLAuthorizationStatusAuthorizedWhenInUse: NSLog(@"AuthorizedWhenInUse"); break; case kCLAuthorizationStatusDenied: NSLog(@"Denied"); break; case kCLAuthorizationStatusNotDetermined: NSLog(@"not Determined"); break; case kCLAuthorizationStatusRestricted: NSLog(@"Restricted"); break; default: break; } }
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{ switch (status) { case kCLAuthorizationStatusAuthorizedAlways: NSLog(@"Always Authorized"); break; case kCLAuthorizationStatusAuthorizedWhenInUse: NSLog(@"AuthorizedWhenInUse"); break; case kCLAuthorizationStatusDenied: NSLog(@"Denied"); break; case kCLAuthorizationStatusNotDetermined: NSLog(@"not Determined"); break; case kCLAuthorizationStatusRestricted: NSLog(@"Restricted"); break; default: break; }
}
|
设置权限
1 2 3 4 5 6
| - (void)setTheCurrentLocationEnvironment { CLLocationManager *manager = [[CLLocationManager alloc] init]; [manager requestAlwaysAuthorization]; [manager requestWhenInUseAuthorization]; }
|
5. 推送权限
获取状态
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| - (void)getTheCurrentNotificationEnvironment { UIUserNotificationSettings *settings = [[UIApplication sharedApplication] currentUserNotificationSettings]; switch (settings.types) { case UIUserNotificationTypeNone: NSLog(@"None"); break; case UIUserNotificationTypeAlert: NSLog(@"Alert Notification"); break; case UIUserNotificationTypeBadge: NSLog(@"Badge Notification"); break; case UIUserNotificationTypeSound: NSLog(@"sound Notification'"); break;
default: break; } }
|
设置权限
1 2 3 4 5
| - (void)setTheCurrentNotificationEnvironment { UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:setting]; }
|
6. 通讯录权限
依赖库
@import AddressBook; // 通讯录权限 iOS 9-
@import Contacts; // 通讯录权限 iOS 9+
获取状态
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
| - (void)getTheCurrentAddressBookEnvironmentOld {
ABAuthorizationStatus ABstatus = ABAddressBookGetAuthorizationStatus(); switch (ABstatus) { case kABAuthorizationStatusAuthorized: NSLog(@"Authorized"); break; case kABAuthorizationStatusDenied: NSLog(@"Denied'"); break; case kABAuthorizationStatusNotDetermined: NSLog(@"not Determined"); break; case kABAuthorizationStatusRestricted: NSLog(@"Restricted"); break; default: break; } }
- (void)getTheCurrentAddressBookEnvironment {
CNAuthorizationStatus CNstatus = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts]; switch (CNstatus) { case CNAuthorizationStatusAuthorized: NSLog(@"Authorized"); break; case CNAuthorizationStatusDenied: NSLog(@"Denied'"); break; case CNAuthorizationStatusNotDetermined: NSLog(@"not Determined"); break; case CNAuthorizationStatusRestricted: NSLog(@"Restricted"); break; default: break; } }
|
设置权限
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
| - (void)setTheCurrentAddressBookEnvironmentOld {
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL); ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) { if (granted) { NSLog(@"Authorized"); CFRelease(addressBook); }else{ NSLog(@"Denied or Restricted"); } }); }
- (void)setTheCurrentAddressBookEnvironment { CNContactStore *store = [[CNContactStore alloc] init]; [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) { if (granted) { NSLog(@"Authorized"); }else{ NSLog(@"Denied or Restricted"); } }]; }
|
7. 日历、备忘录权限
依赖库
@import EventKit; // 日历、备忘录权限
获取状态
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| - (void)getTheCurrentDayEventEnvironment { EKAuthorizationStatus EKstatus = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent]; switch (EKstatus) { case EKAuthorizationStatusAuthorized: NSLog(@"Authorized"); break; case EKAuthorizationStatusDenied: NSLog(@"Denied'"); break; case EKAuthorizationStatusNotDetermined: NSLog(@"not Determined"); break; case EKAuthorizationStatusRestricted: NSLog(@"Restricted"); break; default: break; } }
|
设置权限
1 2 3 4 5 6 7 8 9 10 11
| - (void)setTheCurrentDayEventEnvironment { EKEventStore *store = [[EKEventStore alloc]init]; [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError * _Nullable error) { if (granted) { NSLog(@"Authorized"); }else{ NSLog(@"Denied or Restricted"); } }]; }
|
8. 彩蛋: 跳转App权限界面
1 2 3 4
| - (void)setTheCurrentAllEnvironment { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]; }
|
总结
希望大家能够玩转设备权限