美文网首页
iOS 访问“健康”

iOS 访问“健康”

作者: 黄善军Jackie | 来源:发表于2019-01-23 03:42 被阅读8次

    一、访问“健康”正常的步骤:

    1、在功能(capabilities)列表中打开HealthKit

    capabilities
    打开后,会创建一个后缀为entitlements的文件,它是个字典文件:
    entitlements

    2、在info.plist中添加相关键值:

    NSHealthUpdateUsageDescription 请求更新健康数据
    NSHealthShareUsageDescription 请求访问健康数据

    info.plist

    3、直接上代码:

    
     if ([HKHealthStore isHealthDataAvailable])  {
    
            // 步数
            HKQuantityType *stepCountType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
            // 步行 + 跑步距离
            HKQuantityType *walkingRunningType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceWalkingRunning];
            // 活动能量
            HKQuantityType *activeEnergyType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierActiveEnergyBurned];
    
            // 睡眠分析
            HKCategoryType *sleepType = [HKObjectType categoryTypeForIdentifier:HKCategoryTypeIdentifierSleepAnalysis];
    
            // 出生日期
            HKCharacteristicType *birthdayType = [HKObjectType characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierDateOfBirth];
    
            NSMutableSet<HKObjectType *> *readDataTypes = [NSMutableSet setWithObjects:birthdayType, sleepType, stepCountType, walkingRunningType, activeEnergyType, nil];
            NSMutableSet<HKSampleType *> *shareDataTypes = [NSMutableSet setWithObjects:stepCountType, walkingRunningType, activeEnergyType, sleepType, nil];
    
            if (@available(iOS 9.0, *)) {
                // 月经
                HKCategoryType *menstrualType = [HKObjectType categoryTypeForIdentifier:HKCategoryTypeIdentifierMenstrualFlow];
    
                [readDataTypes addObject:menstrualType];
                [shareDataTypes addObject:menstrualType];
            }
    
            HKHealthStore *healthStore = [[HKHealthStore alloc] init];
            [healthStore requestAuthorizationToShareTypes:shareDataTypes readTypes:readDataTypes completion:^(BOOL success, NSError * _Nullable error) {
    
                if (success) {
    
                    // 操作数据
                } else {
    
                   [self alertWithTitle:@"需要您授权访问健康中心" message:@"设置方法:打开手机设置->隐私->位置信息" okTitle:@"打开设置" cancelTitle:@"取消" okHandler:^(UIAlertAction *action) {
                      NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
                      if (@available(iOS 10.0, *)) {
                        [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
                      } else {
                        if ([[UIApplication sharedApplication] canOpenURL:url]) {
                          [[UIApplication sharedApplication] openURL:url];
                        }
                      }
                    }];
                }
            }];
        } else {
    
            NSError *error = [NSError errorWithDomain: @"com.project.healthkit" code:2 userInfo: [NSDictionary dictionaryWithObject:@"HealthKit is not available in this Device"                                                                      forKey:NSLocalizedDescriptionKey]];
        }
    

    二、问题:

    1、已经按以上步骤操作了,还是会出现崩溃:

    *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'NSHealthUpdateUsageDescription must be set in the app's Info.plist in order to request write authorization for the following types: HKQuantityTypeIdentifierActiveEnergyBurned, HKCategoryTypeIdentifierMenstrualFlow, HKQuantityTypeIdentifierDistanceWalkingRunning, HKCategoryTypeIdentifierSleepAnalysis, HKQuantityTypeIdentifierStepCount'

    之前我也一直运行得没什么问题,忽然有一天就不行了,可能是更新Xcode的什么的吧,这个问题搞得很是头疼,各种尝试后,发现是修改描述的语言就OK了:


    描述换成英文

    对,就是把中文换成英文,坑爹了吧

    相关文章

      网友评论

          本文标题:iOS 访问“健康”

          本文链接:https://www.haomeiwen.com/subject/xcvdjqtx.html