代码之家  ›  专栏  ›  技术社区  ›  satya

如何在iOS的HealthKit中保存HKQuantityTypeIdentifierBodyMass类型的样本

  •  2
  • satya  · 技术社区  · 10 年前

    我试图获得授权保存类型的样本 HKQuantityTypeIdentifierBodyMass: HKCharacteristicTypeIdentifierDateOfBirth

    我的代码是,

    NSArray *readTypes = @[[HKObjectType   
        characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierDateOfBirth]];
    
    NSArray *writeTypes = @[[HKObjectType 
        quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass]];
    
    [self.healthStore requestAuthorizationToShareTypes:[NSSet setWithArray:readTypes]
        readTypes:[NSSet setWithArray:writeTypes] completion:nil];
    

    当我运行此代码时,会出现异常:

    由于未捕获的异常“NSInvalidArgumentException”而终止应用,原因:“不允许授权共享以下类型:HKCharacteristicTypeIdentifierDateOfBirth。”。

    我正在跑步 iOS 9.2 Xcode 7.2 如有任何帮助,我们将不胜感激。

    1 回复  |  直到 10 年前
        1
  •  2
  •   Akhilrajtr    10 年前

    根据文件 requestAuthorizationToShareTypes

    typesToShare包括一个包含要共享的数据类型的集合。此集合可以包含 HKSampleType

    typesToRead包括一个包含要读取的数据类型的集合。此集合可以包含 HKObjectType

    所以在你的情况下,

    NSArray *readTypes = @[[HKObjectType   
        characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierDateOfBirth]];
    
    NSArray *writeTypes = @[[HKObjectType 
        quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass]];
    

    或者尝试,

    [self.healthStore requestAuthorizationToShareTypes:[NSSet setWithArray:writeTypes]
        readTypes:[NSSet setWithArray:readTypes] completion:nil];
    

    或尝试

    NSArray *readTypes = @[[HKObjectType   
        characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierDateOfBirth], [HKObjectType 
        quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass]];
    [self.healthStore requestAuthorizationToShareTypes:nil
        readTypes:[NSSet setWithArray:readTypes] completion:nil];