iOS 探讨之 玩转设备权限

阐述

  从前几年的棱镜门到最近的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
// 获取当前相册权限 iOS 6 - 9
- (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;
}
}

// 获取当前相册权限 iOS 8+
- (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
// 设置相册权限 iOS 8+
- (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];//相机权限
// AVAuthorizationStatus AVstatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];//麦克风权限

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

// [AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio 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
// 获取当前地址薄权限 iOS 9-
- (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;
}
}

// 获取当前地址薄权限 iOS 9+
- (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
// 设置当前地址薄权限 iOS 9-
- (void)setTheCurrentAddressBookEnvironmentOld {

ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
if (granted) {
NSLog(@"Authorized");
CFRelease(addressBook);
}else{
NSLog(@"Denied or Restricted");
}
});
}

// 设置当前地址薄权限 iOS 9+
- (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
// 跳转至系统设置当前App权限界面
- (void)setTheCurrentAllEnvironment {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}



总结

希望大家能够玩转设备权限

文章目录
,