<!--
{
  "availability" : [
    "iOS: 16.0.0 -",
    "iPadOS: 16.0.0 -",
    "macCatalyst: 16.0.0 -",
    "macOS: 13.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 9.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "HealthKit",
  "identifier" : "/documentation/HealthKit/HKAttachment",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "HealthKit"
    ],
    "preciseIdentifier" : "c:objc(cs)HKAttachment"
  },
  "title" : "HKAttachment"
}
-->

# HKAttachment

A file that is attached to a sample in the HealthKit store.

```
class HKAttachment
```

## Overview

To access the attachment’s data, get a data reader from the attachment store for each attachment.

```swift
let attachmentStore = HKAttachmentStore(healthStore: store)

let attachments: [HKAttachment]
do {
    attachments = try await attachmentStore.attachments(for: prescription)
} catch {
    // Handle the error here.
    fatalError("*** An error occurred while accessing the attachments for a prescription: \(error.localizedDescription) ***")
}

// Use the attachments here.
print("*** \(attachments.count) attachments found ***")

for attachment in attachments {

    // Get a data reader for the attachment.
    let dataReader = attachmentStore.dataReader(for:   attachment)

    // Read the data here.
}
```

You can then asynchronously access the whole data object.

```swift
let data: Data
do {
    data = try await dataReader.data
} catch {
    // Handle the error here.
    fatalError("*** An error occurred while accessing the attachment's data. \(error.localizedDescription) ***")
}
```

Alternatively, you can access the file’s contents as an asynchronous sequence of bytes.

```swift
// Asynchronously access the attachment's bytes.
var data = Data()
do {
    for try await byte in dataReader.bytes {
        // Use the bytes here.
        data.append(byte)
    }
} catch {
    // Handle the error here.
    fatalError("*** An error occurred while reading the attachment's data: \(error.localizedDescription) ***")
}
```

> Note:
> You can only add attachments to ``doc://com.apple.healthkit/documentation/HealthKit/HKVisionPrescription``, ``doc://com.apple.healthkit/documentation/HealthKit/HKGlassesPrescription``, and ``doc://com.apple.healthkit/documentation/HealthKit/HKContactsPrescription`` samples. You can also read attachments from ``doc://com.apple.healthkit/documentation/HealthKit/HKClinicalTypeIdentifier/clinicalNoteRecord`` samples.

## Topics

### Accessing attachment data

[`var name: String`](/documentation/HealthKit/HKAttachment/name)

The name of the attached file.

[`var identifier: UUID`](/documentation/HealthKit/HKAttachment/identifier)

The universally unique identifier for the attached file.

[`var contentType: UTType`](/documentation/HealthKit/HKAttachment/contentType)

The type of data stored in the attached file.

[`var size: Int`](/documentation/HealthKit/HKAttachment/size)

The attachment’s size (in bytes).

[`var creationDate: Date`](/documentation/HealthKit/HKAttachment/creationDate)

The attachment’s creation date.

[`var metadata: [String : Any]?`](/documentation/HealthKit/HKAttachment/metadata)

Additional data associated with the attachment in the HealthKit store.

[`struct AsyncBytes`](/documentation/HealthKit/HKAttachment/AsyncBytes)

An asynchronous sequence that returns the attached file as a series of bytes.

### Initializers

[`init?(coder: NSCoder)`](/documentation/HealthKit/HKAttachment/init(coder:))

## See Also

[`class HKVisionPrescription`](/documentation/HealthKit/HKVisionPrescription)

A sample that stores a vision prescription.

[`class HKGlassesPrescription`](/documentation/HealthKit/HKGlassesPrescription)

A sample that stores a prescription for glasses.

[`class HKContactsPrescription`](/documentation/HealthKit/HKContactsPrescription)

A sample that store a prescription for contacts.

## Relationships

### Inherits From

[`NSObject-swift.class`](/documentation/ObjectiveC/NSObject-swift.class)

### Conforms To

[`NSCoding`](/documentation/Foundation/NSCoding)

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

[`SendableMetatype`](/documentation/Swift/SendableMetatype)

[`NSCopying`](/documentation/Foundation/NSCopying)

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

[`CVarArg`](/documentation/Swift/CVarArg)

[`NSObjectProtocol`](/documentation/ObjectiveC/NSObjectProtocol)

[`Equatable`](/documentation/Swift/Equatable)

[`Sendable`](/documentation/Swift/Sendable)

[`NSSecureCoding`](/documentation/Foundation/NSSecureCoding)

[`Hashable`](/documentation/Swift/Hashable)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)