首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SWIFT:从CDA到iOS 10健康应用

SWIFT:从CDA到iOS 10健康应用
EN

Stack Overflow用户
提问于 2016-09-09 16:13:20
回答 2查看 564关注 0票数 0

有没有办法将xml文件(CDA)从另一个应用程序发送到健康应用程序?因为我的应用程序正在从源获取xml文件,并且我希望将其直接发送到新的健康应用程序(iOS 10)。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-09-10 13:17:04

创建HKCDADocumentSample并使用HKHealthStore保存它。

票数 0
EN

Stack Overflow用户

发布于 2017-01-03 12:21:26

首先,检查授权

代码语言:javascript
复制
(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];
  }

}

其次,添加记录wmthod这将添加一个健康记录到健康应用程序。

代码语言:javascript
复制
(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");
}];

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39406871

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档