iOS开发检测是否开启定位、是否允许消息推送等权限的实例
1.iOS开发检测是否开启定位:
需要导入:
#import
代码如下:
+(void)openLocationServiceWithBlock:(ReturnBlock)returnBlock
{
BOOLisOPen=NO;
if([CLLocationManagerlocationServicesEnabled]&&[CLLocationManagerauthorizationStatus]!=kCLAuthorizationStatusDenied){
isOPen=YES;
}
if(returnBlock){
returnBlock(isOpen);
}
}
2.iOS开发检测是否允许消息推送:
需要导入:
#import
代码如下:
+(void)openMessageNotificationServiceWithBlock:(ReturnBlock)returnBlock
{
BOOLisOpen=NO;
#if__IPHONE_OS_VERSION_MAX_ALLOWED>=__IPHONE_8_0
UIUserNotificationSettings*setting=[[UIApplicationsharedApplication]currentUserNotificationSettings];
if(setting.types!=UIUserNotificationTypeNone){
isOpen=YES;
}
#else
UIRemoteNotificationTypetype=[[UIApplicationsharedApplication]enabledRemoteNotificationTypes];
if(type!=UIRemoteNotificationTypeNone){
isOpen=YES;
}
#endif
if(returnBlock){
returnBlock(isOpen);
}
}
+(void)openMessageNotificationServiceWithBlock:(ReturnBlock)returnBlock
{
#if__IPHONE_OS_VERSION_MAX_ALLOWED>=__IPHONE_10_0
[[UNUserNotificationCentercurrentNotificationCenter]getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings*settings){
if(returnBlock){
returnBlock(settings.authorizationStatus==UNAuthorizationStatusAuthorized);
}
}];
#elif__IPHONE_OS_VERSION_MAX_ALLOWED>=__IPHONE_8_0
returnBlock([[UIApplicationsharedApplication]isRegisteredForRemoteNotifications]);
#else
UIRemoteNotificationTypetype=[[UIApplicationsharedApplication]enabledRemoteNotificationTypes];
if(returnBlock){
returnBlock(type!=UIRemoteNotificationTypeNone);
}
#endif
}
3.iOS开发检测是否开启摄像头:
需要导入:
#import
代码如下:
+(void)openCaptureDeviceServiceWithBlock:(ReturnBlock)returnBlock
{
#if__IPHONE_OS_VERSION_MAX_ALLOWED>=__IPHONE_7_0
AVAuthorizationStatusauthStatus=[AVCaptureDeviceauthorizationStatusForMediaType:AVMediaTypeVideo];
if(authStatus==AVAuthorizationStatusNotDetermined){
[AVCaptureDevicerequestAccessForMediaType:AVMediaTypeVideocompletionHandler:^(BOOLgranted){
if(returnBlock){
returnBlock(granted);
}
}];
returnNO;
}elseif(authStatus==AVAuthorizationStatusRestricted||authStatus==AVAuthorizationStatusDenied){
returnBlock(NO);
}else{
returnBlock(YES);
}
#endif
}
4.iOS开发检测是否开启相册:
需要导入:
#import#import
代码如下:
+(void)openAlbumServiceWithBlock:(ReturnBlock)returnBlock
{
BOOLisOpen;
#if__IPHONE_OS_VERSION_MAX_ALLOWED>=__IPHONE_8_0
PHAuthorizationStatusauthStatus=[PHPhotoLibraryauthorizationStatus];
isOpen=YES;
if(authStatus==PHAuthorizationStatusRestricted||authStatus==PHAuthorizationStatusDenied){
isOpen=NO;
}
#else
ALAuthorizationStatusauthor=[ALAssetsLibraryauthorizationStatus];
isOpen=YES;
if(author==ALAuthorizationStatusRestricted||author==ALAuthorizationStatusDenied){
isOpen=NO;
}
#endif
if(returnBlock){
returnBlock(isOpen);
}
}
5.iOS开发检测是否开启麦克风:
需要导入:
#import
代码如下:
+(void)openRecordServiceWithBlock:(ReturnBlock)returnBlock
{
#if__IPHONE_OS_VERSION_MAX_ALLOWED>=__IPHONE_8_0
AVAudioSessionRecordPermissionpermissionStatus=[[AVAudioSessionsharedInstance]recordPermission];
if(permissionStatus==AVAudioSessionRecordPermissionUndetermined){
[[AVAudioSessionsharedInstance]requestRecordPermission:^(BOOLgranted){
if(returnBlock){
returnBlock(granted);
}
}];
}elseif(permissionStatus==AVAudioSessionRecordPermissionDenied){
returnBlock(NO);
}else{
returnBlock(YES);
}
#endif
}
6.iOS开发检测是否开启通讯录:
需要导入:
#import#import
代码如下:
+(void)openContactsServiceWithBolck:(ReturnBlock)returnBolck
{
#if__IPHONE_OS_VERSION_MAX_ALLOWED>=__IPHONE_9_0
CNAuthorizationStatuscnAuthStatus=[CNContactStoreauthorizationStatusForEntityType:CNEntityTypeContacts];
if(cnAuthStatus==CNAuthorizationStatusNotDetermined){
CNContactStore*store=[[CNContactStorealloc]init];
[storerequestAccessForEntityType:CNEntityTypeContactscompletionHandler:^(BOOLgranted,NSError*error){
if(returnBolck){
returnBolck(granted);
}
}];
}elseif(cnAuthStatus==CNAuthorizationStatusRestricted||cnAuthStatus==CNAuthorizationStatusDenied){
if(returnBolck){
returnBolck(NO);
}
}else{
if(returnBolck){
returnBolck(YES);
}
}
#else
//ABAddressBookRefaddressBook=ABAddressBookCreateWithOptions(NULL,NULL);
ABAddressBookRefaddressBook=ABAddressBookCreate();
ABAuthorizationStatusauthStatus=ABAddressBookGetAuthorizationStatus();
if(authStatus!=kABAuthorizationStatusAuthorized){
ABAddressBookRequestAccessWithCompletion(addressBook,^(boolgranted,CFErrorReferror){
dispatch_async(dispatch_get_main_queue(),^{
if(error){
NSLog(@"Error:%@",(__bridgeNSError*)error);
if(returnBolck){
returnBolck(NO);
}
}else{
if(returnBolck){
returnBolck(YES);
}
}
});
});
}else{
if(returnBolck){
returnBolck(YES);
}
}
#endif
}
7.iOS开发检测是否开启蓝牙:
需要导入:
#import
代码如下:
+(void)openPeripheralServiceWithBolck:(ReturnBlock)returnBolck
{
#if__IPHONE_OS_VERSION_MAX_ALLOWED>=__IPHONE_7_0
CBPeripheralManagerAuthorizationStatuscbAuthStatus=[CBPeripheralManagerauthorizationStatus];
if(cbAuthStatus==CBPeripheralManagerAuthorizationStatusNotDetermined){
if(returnBolck){
returnBolck(NO);
}
}elseif(cbAuthStatus==CBPeripheralManagerAuthorizationStatusRestricted||cbAuthStatus==CBPeripheralManagerAuthorizationStatusDenied){
if(returnBolck){
returnBolck(NO);
}
}else{
if(returnBolck){
returnBolck(YES);
}
}
#endif
}
8.iOS开发检测是否开启日历/备忘录:
需要导入:
#import
代码如下:
+(void)openEventServiceWithBolck:(ReturnBlock)returnBolckwithType:(EKEntityType)entityType
{
//EKEntityTypeEvent代表日历
//EKEntityTypeReminder代表备忘
EKAuthorizationStatusekAuthStatus=[EKEventStoreauthorizationStatusForEntityType:entityType];
if(ekAuthStatus==EKAuthorizationStatusNotDetermined){
EKEventStore*store=[[EKEventStorealloc]init];
[storerequestAccessToEntityType:entityTypecompletion:^(BOOLgranted,NSError*error){
if(returnBolck){
returnBolck(granted);
}
}];
}elseif(ekAuthStatus==EKAuthorizationStatusRestricted||ekAuthStatus==EKAuthorizationStatusDenied){
if(returnBolck){
returnBolck(NO);
}
}else{
if(returnBolck){
returnBolck(YES);
}
}
}
9.iOS开发检测是否开启联网:
需要导入:
#import
代码如下:
+(void)openEventServiceWithBolck:(ReturnBlock)returnBolck
{
#if__IPHONE_OS_VERSION_MAX_ALLOWED>=__IPHONE_9_0
CTCellularData*cellularData=[[CTCellularDataalloc]init];
cellularData.cellularDataRestrictionDidUpdateNotifier=^(CTCellularDataRestrictedStatestate){
if(state==kCTCellularDataRestrictedStateUnknown||state==kCTCellularDataNotRestricted){
if(returnBolck){
returnBolck(NO);
}
}else{
if(returnBolck){
returnBolck(YES);
}
}
};
CTCellularDataRestrictedStatestate=cellularData.restrictedState;
if(state==kCTCellularDataRestrictedStateUnknown||state==kCTCellularDataNotRestricted){
if(returnBolck){
returnBolck(NO);
}
}else{
if(returnBolck){
returnBolck(YES);
}
}
#endif
}
10.iOS开发检测是否开启健康:
需要导入:
#import
代码如下:
+(void)openHealthServiceWithBolck:(ReturnBlock)returnBolck
{
#if__IPHONE_OS_VERSION_MAX_ALLOWED>=__IPHONE_8_0
if(![HKHealthStoreisHealthDataAvailable]){
if(returnBolck){
returnBolck(NO);
}
}else{
HKHealthStore*healthStore=[[HKHealthStorealloc]init];
HKObjectType*hkObjectType=[HKObjectTypequantityTypeForIdentifier:HKQuantityTypeIdentifierHeight];
HKAuthorizationStatushkAuthStatus=[healthStoreauthorizationStatusForType:hkObjectType];
if(hkAuthStatus==HKAuthorizationStatusNotDetermined){
//1.你创建了一个NSSet对象,里面存有本篇教程中你将需要用到的从HealthStroe中读取的所有的类型:个人特征(血液类型、性别、出生日期)、数据采样信息(身体质量、身高)以及锻炼与健身的信息。
NSSet*healthKitTypesToRead=[[NSSetalloc]initWithArray:@[[HKObjectTypecharacteristicTypeForIdentifier:HKCharacteristicTypeIdentifierDateOfBirth],[HKObjectTypecharacteristicTypeForIdentifier:HKCharacteristicTypeIdentifierBloodType],[HKObjectTypecharacteristicTypeForIdentifier:HKCharacteristicTypeIdentifierBiologicalSex],[HKObjectTypequantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass],[HKObjectTypequantityTypeForIdentifier:HKQuantityTypeIdentifierHeight],[HKObjectTypeworkoutType]]];
//2.你创建了另一个NSSet对象,里面有你需要向Store写入的信息的所有类型(锻炼与健身的信息、BMI、能量消耗、运动距离)
NSSet*healthKitTypesToWrite=[[NSSetalloc]initWithArray:@[[HKObjectTypequantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMassIndex],[HKObjectTypequantityTypeForIdentifier:HKQuantityTypeIdentifierActiveEnergyBurned],[HKObjectTypequantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceWalkingRunning],[HKObjectTypeworkoutType]]];
[healthStorerequestAuthorizationToShareTypes:healthKitTypesToWritereadTypes:healthKitTypesToReadcompletion:^(BOOLsuccess,NSError*error){
if(returnBolck){
returnBolck(success);
}
}];
}elseif(hkAuthStatus==HKAuthorizationStatusSharingDenied){
if(returnBolck){
returnBolck(NO);
}
}else{
if(returnBolck){
returnBolck(YES);
}
}
}
#endif
}
11.iOS开发检测是否开启TouchID:
需要导入:
#import
代码如下:
#pragmamark-开启TouchID服务
+(void)openTouchIDServiceWithBlock:(ReturnBlock)returnBlock
{
#if__IPHONE_OS_VERSION_MAX_ALLOWED>=__IPHONE_8_0
LAContext*laContext=[[LAContextalloc]init];
laContext.localizedFallbackTitle=@"输入密码";
NSError*error;
if([laContextcanEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometricserror:&error]){
NSLog(@"恭喜,TouchID可以使用!");
[laContextevaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometricslocalizedReason:@"需要验证您的指纹来确认您的身份信息"reply:^(BOOLsuccess,NSError*error){
if(success){
//识别成功
if(returnBlock){
[[NSOperationQueuemainQueue]addOperationWithBlock:^{
returnBlock(YES);
}];
}
}elseif(error){
if(returnBlock){
[[NSOperationQueuemainQueue]addOperationWithBlock:^{
returnBlock(NO);
}];
}
if(error.code==LAErrorAuthenticationFailed){
//验证失败
}
if(error.code==LAErrorUserCancel){
//用户取消
}
if(error.code==LAErrorUserFallback){
//用户选择输入密码
}
if(error.code==LAErrorSystemCancel){
//系统取消
}
if(error.code==LAErrorPasscodeNotSet){
//密码没有设置
}
}
}];
}else{
NSLog(@"设备不支持TouchID功能,原因:%@",error);
if(returnBlock){
returnBlock(NO);
}
}
#endif
}
12.iOS开发检测是否开启ApplePay:
需要导入:
#import
代码如下:
#pragmamark-开启ApplePay服务
+(void)openApplePayServiceWithBlock:(ReturnBlock)returnBlock
{
#if__IPHONE_OS_VERSION_MAX_ALLOWED>=__IPHONE_9_0
NSArray*supportedNetworks=@[PKPaymentNetworkAmex,PKPaymentNetworkMasterCard,PKPaymentNetworkVisa,PKPaymentNetworkDiscover];
if([PKPaymentAuthorizationViewControllercanMakePayments]&&[PKPaymentAuthorizationViewControllercanMakePaymentsUsingNetworks:supportedNetworks]){
if(returnBlock){
returnBlock(YES);
}
}else{
if(returnBlock){
returnBlock(NO);
}
}
#endif
}
13.iOS开发检测是否开启语音识别:
需要导入:
#import
代码如下:
#pragmamark-开启语音识别服务
+(void)openSpeechServiceWithBlock:(ReturnBlock)returnBlock
{
#if__IPHONE_OS_VERSION_MAX_ALLOWED>=__IPHONE_10_0
SFSpeechRecognizerAuthorizationStatusspeechAuthStatus=[SFSpeechRecognizerauthorizationStatus];
if(speechAuthStatus==SFSpeechRecognizerAuthorizationStatusNotDetermined){
[SFSpeechRecognizerrequestAuthorization:^(SFSpeechRecognizerAuthorizationStatusstatus){
if(status==SFSpeechRecognizerAuthorizationStatusAuthorized){
dispatch_async(dispatch_get_main_queue(),^{
if(returnBlock){
returnBlock(YES);
}
});
}else{
dispatch_async(dispatch_get_main_queue(),^{
if(returnBlock){
returnBlock(YES);
}
});
}
}];
}elseif(speechAuthStatus==SFSpeechRecognizerAuthorizationStatusAuthorized){
if(returnBlock){
returnBlock(YES);
}
}else{
if(returnBlock){
returnBlock(NO);
}
}
#endif
}
14.iOS开发检测是否开启媒体资料库/AppleMusic:
需要导入:
#import
代码如下:
#pragmamark-开启媒体资料库/AppleMusic服务
+(void)openMediaPlayerServiceWithBlock:(ReturnBlock)returnBlock
{
#if__IPHONE_OS_VERSION_MAX_ALLOWED>=__IPHONE_9_3
MPMediaLibraryAuthorizationStatusauthStatus=[MPMediaLibraryauthorizationStatus];
if(authStatus==MPMediaLibraryAuthorizationStatusNotDetermined){
[MPMediaLibraryrequestAuthorization:^(MPMediaLibraryAuthorizationStatusstatus){
if(status==MPMediaLibraryAuthorizationStatusAuthorized){
dispatch_async(dispatch_get_main_queue(),^{
if(returnBlock){
returnBlock(YES);
}
});
}else{
dispatch_async(dispatch_get_main_queue(),^{
if(returnBlock){
returnBlock(NO);
}
});
}
}];
}elseif(authStatus==MPMediaLibraryAuthorizationStatusAuthorized){
if(returnBlock){
returnBlock(YES);
}
}else{
if(returnBlock){
returnBlock(NO);
}
}
#endif
}
15.iOS开发检测是否开启Siri:
需要导入:
#import
代码如下:
#pragmamark-开启Siri服务
+(void)openSiriServiceWithBlock:(ReturnBlock)returnBlock
{
#if__IPHONE_OS_VERSION_MAX_ALLOWED>=__IPHONE_10_0
INSiriAuthorizationStatussiriAutoStatus=[INPreferencessiriAuthorizationStatus];
if(siriAutoStatus==INSiriAuthorizationStatusNotDetermined){
[INPreferencesrequestSiriAuthorization:^(INSiriAuthorizationStatusstatus){
if(status==INSiriAuthorizationStatusAuthorized){
dispatch_async(dispatch_get_main_queue(),^{
if(returnBlock){
returnBlock(YES);
}
});
}else{
dispatch_async(dispatch_get_main_queue(),^{
if(returnBlock){
returnBlock(YES);
}
});
}
}];
}elseif(siriAutoStatus==INSiriAuthorizationStatusAuthorized){
if(returnBlock){
returnBlock(YES);
}
}else{
if(returnBlock){
returnBlock(NO);
}
}
#endif
}
在.h文件中申明block
#ifNS_BLOCKS_AVAILABLE typedefvoid(^ReturnBlock)(BOOLisOpen); #endif
由于iOS10的权限原因,需要在工程的info.plist(右击选择Openas-SourceCode)中添加
NSPhotoLibraryUsageDescription App需要您的同意,才能访问相册 NSCameraUsageDescription App需要您的同意,才能访问相机 NSMicrophoneUsageDescription App需要您的同意,才能访问麦克风 NSLocationUsageDescription App需要您的同意,才能访问位置 NSLocationWhenInUseUsageDescription App需要您的同意,才能在使用期间访问位置 NSLocationAlwaysUsageDescription App需要您的同意,才能始终访问位置 NSCalendarsUsageDescription App需要您的同意,才能访问日历 NSRemindersUsageDescription App需要您的同意,才能访问提醒事项 NSMotionUsageDescription App需要您的同意,才能访问运动与健身 NSHealthUpdateUsageDescription App需要您的同意,才能访问健康更新 NSHealthShareUsageDescription App需要您的同意,才能访问健康分享 NSBluetoothPeripheralUsageDescription App需要您的同意,才能访问蓝牙 NSAppleMusicUsageDescription App需要您的同意,才能访问媒体资料库 NSSpeechRecognitionUsageDescription App需要您的同意,才能使用语音识别
以上这篇iOS开发检测是否开启定位、是否允许消息推送等权限的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。