【问题标题】:SWIFT: CDA to iOS 10 Health appSWIFT:CDA 到 iOS 10 健康应用程序
【发布时间】:2016-09-09 08:13:19
【问题描述】:

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

【问题讨论】:

    标签: swift healthkit cda


    【解决方案1】:

    创建一个HKCDADocumentSample 并用HKHealthStore 保存它。

    【讨论】:

    • 您知道如何访问和显示存储在 healthkit 中的所有可用健康记录吗?
    【解决方案2】:

    首先,检查授权

    (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");
    }];
    
    }
    

    【讨论】:

    • 你能用swift语言做吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-15
    • 1970-01-01
    • 2018-07-07
    • 1970-01-01
    • 2023-02-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多