Tell me, if the Health Kit sample came from the Apple Watch?

The Health app displays the Watch icon when the source was Apple Watch.

I would just like to get the same information that the Health application uses to determine the type of source. HKSource does not seem to provide this.

+4
source share
2 answers

I had a similar problem in my application where we needed to take steps only for the Apple iPhone and Apple Watch. I searched a lot, but could not find an answer to it for iOS 8.0 and landed on your question.

I figured out a way to distinguish between a watch and a phone using the following process (maybe not the best solution, but it works in my situation):

, , iPhone/Watch, bundleIdentifier:

com.apple.health.DeviceUUID

, "" com.apple.Health( "H" ).

, -, , :

NSString *deviceName = [[UIDevice currentDevice] name];

, "com.apple.health" bundleIdentifier. iPhone Apple .

, (iPhone), Apple Watch.

:

- (void)fetchSources 
{
    NSString *deviceName = [[UIDevice currentDevice] name];
    NSMutableArray *dataSources = [[NSMutableArray alloc] init];
    HKQuantityType *stepsCount = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
    HKSourceQuery *sourceQuery = [[HKSourceQuery alloc] initWithSampleType:stepsCount
                                                           samplePredicate:nil
                                                         completionHandler:^(HKSourceQuery *query, NSSet *sources, NSError *error)
                                                         {
                                                             for (HKSource *source in sources)
                                                             {
                                                                 //Ignore the iPhone as a source as the name of the device will watch with the source.
                                                                 //The other device will be an Apple Watch   
                                                                 if ([source.bundleIdentifier hasPrefix:sourceIdentifier] && ![source.name isEqualToString:deviceName])
                                                                 {
                                                                     [dataSources addObject:source];
                                                                 }
                                                             }
                                                         }];
    [self.healthStore executeQuery:sourceQuery];
}

NSPredicate:

NSPredicate *sourcesPredicate = [HKQuery predicateForObjectsFromSource:source];

, , UUID, UUID NSUUID, , .

, , , "".

, , , , . , , . .

+4

iOS 9, HKSample device HKDevice.

https://developer.apple.com/library/prerelease/ios/documentation/HealthKit/Reference/HKDevice_ClassReference/index.html

HKDevice .

HKDevice.model . Apple , Apple HKDevice.model. "iPhone" "Watch".

+4

All Articles