【发布时间】:2016-09-09 08:13:19
【问题描述】:
有没有办法将 xml 文件 (CDA) 从另一个应用程序发送到健康应用程序?因为我的应用程序正在从源获取一个 xml 文件,我想将它直接发送到新的健康应用程序 (iOS 10)。
【问题讨论】:
有没有办法将 xml 文件 (CDA) 从另一个应用程序发送到健康应用程序?因为我的应用程序正在从源获取一个 xml 文件,我想将它直接发送到新的健康应用程序 (iOS 10)。
【问题讨论】:
创建一个HKCDADocumentSample 并用HKHealthStore 保存它。
【讨论】:
首先,检查授权
(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");
}];
}
【讨论】: