有没有办法从另一个应用程序将 xml 文件 (CDA) 发送到健康应用程序?因为我的应用程序正在从源获取 xml 文件,并且我想将其直接发送到新的健康应用程序 (iOS 10)。
问问题
569 次
2 回答
0
创建一个HKCDADocumentSample并将其与HKHealthStore一起保存。
于 2016-09-10T05:17:03.800 回答
0
首先,检查授权
(void) checkForAuthorization {
if ([HKHealthStore isHealthDataAvailable]) {
NSSet *setRead = [NSSet setWithObjects [HKObjectTypedocumentTypeForIdentifier:HKDocumentTypeIdentifierCDA], nil];
NSSet *setWrite = [NSSet setWithObjects:[HKObjectType documentTypeForIdentifier:HKDocumentTypeIdentifierCDA], nil];
[_store requestAuthorizationToShareTypes:setWrite readTypes:nil completion:^(BOOL success, NSError * _Nullable error) {
if (error) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:error.localizedDescription delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
} else if (success) {
[self performSelectorOnMainThread:@selector(addDocumentToHealthApp) withObject:nil waitUntilDone:NO];
}
NSLog(@" Success = %@",success? @"YES" : @"NO");
} ];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Health Kit not supported in device." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
}
}
其次,添加 Record wmthod 这将向健康应用程序添加健康记录。
(void) addRecordToHealthApp
{
NSURL *cdaPath = [[NSBundle mainBundle] URLForResource:@"sample" withExtension:@"xml"];
NSString*stringPath = [cdaPath absoluteString];
NSData *dataOfCDAFile = [NSData dataWithContentsOfURL:[NSURL URLWithString:stringPath]];
NSDate *now = [NSDate date];
int daysToAdd = 7;
NSDate *newDate1 = [now dateByAddingTimeInterval:60*60*24*daysToAdd];
NSError *err;
HKCDADocumentSample *doc = [HKCDADocumentSample CDADocumentSampleWithData:dataOfCDAFile startDate:[NSDate date] endDate:newDate1 metadata:nil validationError:&err ];
UIAlertView *alert;
if (err) {
alert = [[UIAlertView alloc] initWithTitle:@"Error" message:err.localizedDescription delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
}
[_store saveObject:doc withCompletion:^(BOOL success, NSError * _Nullable error) {
NSLog("Stored %@",success?@"YES":@"NO");
}];
}
于 2017-01-03T04:21:25.670 回答