权限分类
- 联网权限
- 相册权限
- 相机 麦克风权限
- 定位权限
- 推送权限
- 通讯录权限
- 日历 备忘录权限
联网权限
- 引入头文件 @import CoreTelephoney
- 应用启动后,检测应用中是否有联网权限
CTCellularData *cellularData = [[CTCellularData alloc]init];
cellularData.cellularDataRestrictionDidUpdateNotifier = ^(CTCellularDataRestrictedState state){
//获取联网状态
switch(state){
case kCellularDataRestricted;
break; //Retricrted
case kCTCellularDataNotRestricted;
break; //Not Restricted
case kCTCellularDataRestrictedStateUnknown;
break; //Unknow
default;
break;
}
}
CTCellularData *cellularData = [CTCellularData alloc]init];
CTCellularDataRestrictedState state = cellularDate.restrictedState;
switch(state){
case kCTCellularDataRestriccted; //Restricrted
break;
case kCTCekkularDateNotResrricted ; //Not Restricted
break;
case cCTCellularDataRestrictedStateUnknow; //Unknow
break;
default;
break;
}
相册权限 (ios9.0 之前)
- 导入头文件 @import AssetsLibrary;
- 检测是否有相册权限
ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];
switch(state)
case ALAuthorizationStatusAuthorized //Authorized;
break
case ALAuthorizationStatusNotDetermined; //DEnied
break
case ALAuthorizationStatusNotDetermined; //Determined
break
case ALAuthorizationStatusTestricted;
break;
default
break
}
相机和麦克风权限
- 导入头文件 @import AVFoundation;
- 检查是否有相机或麦克风权限
AVAuthorizationStatus AVstatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];//相机权限
AVAuthorizationStatus AVstatus = [AVCaotureDevice authorizationStatusForMediaType:AVMediaTypeAudio];//麦克风权限
switch(ACstatus){
case AVAuthorizationStatusAuthorized; //Authorized
break
case AVAuthorizationStatusDenied; //Denied
break
case AVAuthorizationStatusNotDetermined
break
case AVAuthorizationStatusTrstricted; //Restricted
break
datault
break
}
定位权限
- 导入头文件 @import CoreLocation;
- 由于iOS80之后定位方法的改变 需要在info.plost 中进行配置
NSLocationWhenUseUsageDescription String 在使用时获取定位信息
NSLocationAlwaysUsageDescription String 一直获取定位信息
BOOL isLocation = [CLLocationManager locationServicesEnabled];
if(!isLocation){ // not turn on the loaction
}
CLAuthorizationStatus CLstatus = [CLLocationManager authorizationStatus];
switch(CLstatus){
case kClAuthorizationStatusAuthorizedALways; //always Authorized
break;
case kClAuthorizationStatusAuthorizedWhenInUse; //AuthotizationStatusAuthorizedWhenInUse
break;
.......
default
break
}
CLLocation *manager = [CLLocationManager alloc]init];
[manager requestAlwaysAuthorization]; //一直获取定位信息
[manager requestWhenInUseAuthorization]; //使用的时候获取定位信息
最后一点
- 素有获取权限的方法,多用于用户第一次操作应用, iOS 8.0 之后 ,将这些设置都整合在一起, 并且可以开启或关闭相应的权限。所有的权限都可以同下面的方法打开;
[[UIApplication sharedApplication]openURL URLWithString:UIApplicationOpenSettingsURLString]];