iOS开发检测是否开启定位、是否允许消息推送等权限的实例

2020-01-21 04:03:54于海丽


+ (void)openHealthServiceWithBolck:(ReturnBlock)returnBolck
{
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0
 if (![HKHealthStore isHealthDataAvailable]) {
  if (returnBolck) {
   returnBolck(NO);
  }
 } else {
  HKHealthStore *healthStore = [[HKHealthStore alloc] init];
  HKObjectType *hkObjectType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeight];
  HKAuthorizationStatus hkAuthStatus = [healthStore authorizationStatusForType:hkObjectType];
  if (hkAuthStatus == HKAuthorizationStatusNotDetermined) {
   // 1. 你创建了一个NSSet对象,里面存有本篇教程中你将需要用到的从Health Stroe中读取的所有的类型:个人特征(血液类型、性别、出生日期)、数据采样信息(身体质量、身高)以及锻炼与健身的信息。
   NSSet <HKObjectType *> * healthKitTypesToRead = [[NSSet alloc] initWithArray:@[[HKObjectType characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierDateOfBirth],[HKObjectType characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierBloodType],[HKObjectType characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierBiologicalSex],[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass],[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeight],[HKObjectType workoutType]]];
   // 2. 你创建了另一个NSSet对象,里面有你需要向Store写入的信息的所有类型(锻炼与健身的信息、BMI、能量消耗、运动距离)
   NSSet <HKSampleType *> * healthKitTypesToWrite = [[NSSet alloc] initWithArray:@[[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMassIndex],[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierActiveEnergyBurned],[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceWalkingRunning],[HKObjectType workoutType]]];
   [healthStore requestAuthorizationToShareTypes:healthKitTypesToWrite readTypes:healthKitTypesToRead completion:^(BOOL success, NSError *error) {
    if (returnBolck) {
     returnBolck(success);
    }
   }];
  } else if (hkAuthStatus == HKAuthorizationStatusSharingDenied) {
   if (returnBolck) {
    returnBolck(NO);
   }
  } else {
   if (returnBolck) {
    returnBolck(YES);
   }
  }
 }
#endif
}

11.iOS开发检测是否开启Touch ID:

需要导入:


#import <LocalAuthentication/LocalAuthentication.h>


#pragma mark - 开启Touch ID服务
+ (void)openTouchIDServiceWithBlock:(ReturnBlock)returnBlock
{
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0
 LAContext *laContext = [[LAContext alloc] init];
 laContext.localizedFallbackTitle = @"输入密码";
 NSError *error;
 if ([laContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
  NSLog(@"恭喜,Touch ID可以使用!");
  [laContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"需要验证您的指纹来确认您的身份信息" reply:^(BOOL success, NSError *error) {
   if (success) {
    // 识别成功
    if (returnBlock) {
     [[NSOperationQueue mainQueue] addOperationWithBlock:^{
      returnBlock(YES);
     }];
    }
   } else if (error) {
    if (returnBlock) {
     [[NSOperationQueue mainQueue] 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(@"设备不支持Touch ID功能,原因:%@",error);
  if (returnBlock) {
   returnBlock(NO);
  }
 }
#endif
}