init(data:start:end:metadata:)
Returns a CDA document sample containing the provided XML document and metadata.
Declaration
convenience init(data documentData: Data, start startDate: Date, end endDate: Date, metadata: [String : Any]?) throwsParameters
- documentData:
The CDA document in an XML format that meets the CDA standard. For more information on the CDA document format, see the Product Brief.cfm?product Id=7 standard.
- startDate:
A fallback start date for the sample. This date is only used when the XML document does not include the document’s effective date. This date must be equal to or earlier than the end date; otherwise, this method throws an exception (Invalidargumentexception).
- endDate:
A fallback end date for the sample. This date is only used when the XML document does not include the document’s effective date. This date must be equal to or later than the start date; otherwise, this method throws an exception (Invalidargumentexception).
- metadata:
The metadata dictionary contains extra information describing this sample. The dictionary’s keys are all Nsstring objects. The values may be Nsstring objects, Nsnumber objects, or Nsdate objects. For a complete list of predefined metadata keys, see Metadata Keys in Healthkit Constants.
Using predefined keys helps facilitate sharing data between apps; however, you are also encouraged to create your own, custom keys as needed to extend the sample’s capabilities.
Return Value
A valid CDA document sample with the provided metadata.
Discussion
To create a CDA document sample:
Create NSDate objects to represent fallback start and end dates for the sample. Where possible, the system uses the effective date from the document’s XML data to set the sample’s start and end dates. The start and end date parameters are only used when the effective date is not available. Use the current date and time.
Create an NSData object that contains the CDA’s XML data.
(optionally) Create an NSDictionary object containing any additional metadata for this sample.
Call the
HKCDADocumentSampleclass’s init(data:start:end:metadata:) method. Handle any errors that occur during XML validation.Save the sample to the HealthKit store. Handle any errors that occur while saving.
// Creating a Health Document Using HKCDADocumentSample
let today = Date()
let documentData: Data = ... // Use XML data provided by a health organization
do {
let cdaSample = try HKCDADocumentSample.init(data: documentData, start: today, end:
today, metadata: nil)
healthStore.save(cdaSample) { success, error in
// Handle save error here...
}
} catch {
// Handle validation error here...
}