iOS 权限统计

一、权限添加

1、麦克风权限:Privacy - Microphone Usage Description 是否允许此App使用你的麦克风?
2、相机权限: Privacy - Camera Usage Description 是否允许此App使用你的相机?
3、相册权限: Privacy - Photo Library Usage Description 是否允许此App访问你的媒体资料库?
4、通讯录权限: Privacy - Contacts Usage Description 是否允许此App访问你的通讯录?
5、蓝牙权限:Privacy - Bluetooth Peripheral Usage Description 是否许允此App使用蓝牙?
6、语音转文字权限:Privacy - Speech Recognition Usage Description 是否允许此App使用语音识别?
7、日历权限:Privacy - Calendars Usage Description 是否允许此App使用日历?
8、定位权限:Privacy - Location When In Use Usage Description 我们需要通过您的地理位置信息获取您周边的相关数据
9、定位权限: Privacy - Location Always Usage Description 我们需要通过您的地理位置信息获取您周边的相关数据

二、权限获取

1、网络权限

(1)、实时监测网络变化
导入头文件

#import 

//应用启动后,时时检测网络权限变化

CTCellularData *cellularData = [[CTCellularData alloc] init];
/*
 此函数会在网络权限改变时再次调用
 */
cellularData.cellularDataRestrictionDidUpdateNotifier = ^(CTCellularDataRestrictedState state) {
    switch (state) {
        case kCTCellularDataRestricted:
            //关闭————>无线局域网
            //无线局域网与蜂窝移动数据————>无线局域网
            break;
        case kCTCellularDataNotRestricted:
            //关闭————>无线局域网与蜂窝移动数据
            //无线局域网————>无线局域网与蜂窝移动数据
            break;
        case kCTCellularDataRestrictedStateUnknown:
            //未知情况
            break;
        default:
            break;
    }
};

(2)、其他获取网络状态:

方法一:Reachability

①、下载并导入Reachability,这是苹果官方演示demo,把里面的Reachability文件拷贝到自己的工程。下载地址:https://developer.apple.com/library/ios/samplecode/Reachability/Introduction/Intro.html
②、三种网络状态:无网络,wifi和蜂窝网。

typedef enum : NSInteger { 
NotReachable = 0,//没有网络 
ReachableViaWiFi,//当前使用Wifi网络 
ReachableViaWWAN//使用的蜂窝网络 
} NetworkStatus;

③、获取网络状态的代码

Reachability *reachability   = [Reachability reachabilityWithHostName:@"www.apple.com"];
NetworkStatus internetStatus = [reachability currentReachabilityStatus];
switch (internetStatus) {
    case ReachableViaWiFi:
        NSLog(@"wifi");
        break;
    case ReachableViaWWAN:
        NSLog(@"wwan");
        break;
    case NotReachable:
        NSLog(@"not");
    default:
        break;
}

④、时时检测网络变化
//定义变量

@property (nonatomic) Reachability *hostReachability;

//添加监听者

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
NSString *remoteHostName = @"www.apple.com";
self.hostReachability = [Reachability reachabilityWithHostName:remoteHostName];
[self.hostReachability startNotifier];

//监听

- (void) reachabilityChanged:(NSNotification *)note
{
    NetworkStatus internetStatus = [self.hostReachability currentReachabilityStatus];
    switch (internetStatus) {
        case ReachableViaWiFi:
            NSLog(@"wifi");
            break;
        case ReachableViaWWAN:
            NSLog(@"wwan");
            break;
        case NotReachable:
            NSLog(@"not");
        default:
            break;
    }
}

方法二:使用statusbar来获取状态

+ (NSString *)networkingStatesFromStatebar {
    int type = [self networkingStates];
    NSString *stateString = @"wifi";
    switch (type) {
        case 0:
            stateString = @"notReachable";
            break;
        case 1:
            stateString = @"2G";
            break;
        case 2:
            stateString = @"3G";
            break;
        case 3:
            stateString = @"4G";
            break;
        case 4:
            stateString = @"LTE";
            break;
        case 5:
            stateString = @"wifi";
            break;
        default:
            break;
    }
    return stateString;
}
+ (int)networkingStates {
    //状态栏是由当前app控制的,首先获取当前app
    UIApplication *app = [UIApplication sharedApplication];
    NSArray *children = [[[app valueForKeyPath:@"statusBar"] valueForKeyPath:@"foregroundView"] subviews];
    int type = 0;
    for (id child in children) {
        if ([child isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) {
            type = [[child valueForKeyPath:@"dataNetworkType"] intValue];
        }
    }
    return type;
}

方法三:使用AFNetworking

略。。。

2、相机权限

导入头文件

#import 
//获取相机
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (device) {
    //相机权限
    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;
    }
} else {
    //无相机
}
//获取相机权限
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
    if (granted) {
        NSLog(@"Authorized");
    }else{
        NSLog(@"Denied or Restricted");
    }
}];

3、麦克风权限

导入头文件

#import 
//获取麦克风
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
if (device) {
    //麦克风权限
    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;
    }
} else {
    //无麦克风
}
//获取麦克风权限
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
    if (granted) {
        NSLog(@"Authorized");
    }else{
        NSLog(@"Denied or Restricted");
    }
}];

4、相册权限

导入头文件

#import 
//检查是否有相册权限
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;
}
//获取相册权限
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
    if (status == PHAuthorizationStatusAuthorized) {
        NSLog(@"Authorized");
    }else{
        NSLog(@"Denied or Restricted");
    }
}];

5、定位权限

导入头文件

#import 
//检查是否有定位权限
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;
}
//获取定位权限
CLLocationManager *manager = [[CLLocationManager alloc] init];
[manager requestAlwaysAuthorization];//一直获取定位信息
[manager requestWhenInUseAuthorization];//使用的时候获取定位信息

6、推送通知权限

导入头文件

#import 
//检查是否有推送通知权限
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;
}
//获取推送通知权限
UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:setting];

7、通讯录权限

导入头文件

#import 
#import 
//检查是否有通讯录权限
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;
}
//获取通讯录权限
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
    if (granted) {
        NSLog(@"Authorized");
        CFRelease(addressBook);
    }else{
        NSLog(@"Denied or Restricted");
    }
});
//iOS9之后的通讯录框架
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;
}
CNContactStore *contactStore = [[CNContactStore alloc] init];
[contactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
    if (granted) {
        NSLog(@"Authorized");
    }else{
        NSLog(@"Denied or Restricted");
    }
}];

8、日历、备忘录权限

导入头文件

#import 
/**
EKEntityTypeEvent,//日历
EKEntityTypeReminder //备忘
*/
//检查是否有日历或者备忘录权限
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;
}
//获取日历或备忘录权限
EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError * _Nullable error) {
    if (granted) {
        NSLog(@"Authorized");
    }else{
        NSLog(@"Denied or Restricted");
    }
}];

用户拒绝授权后,如果访问相应内容可能会出现一些类似没有数据的情况,此时应该给用户提示,引导用户授权。比如跳转到应用设置:

NSURL *settingUrl = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if ([[UIApplication sharedApplication] canOpenURL:settingUrl]) {
    [[UIApplication sharedApplication] openURL:settingUrl];
}

你可能感兴趣的:(iOS总结)