<!--
{
  "availability" : [
    "iOS: 10.0.0 -",
    "iPadOS: 10.0.0 -",
    "macCatalyst: 13.1.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Messages",
  "identifier" : "/documentation/Messages/MSConversation/insertAttachment(_:withAlternateFilename:completionHandler:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Messages"
    ],
    "preciseIdentifier" : "c:objc(cs)MSConversation(im)insertAttachment:withAlternateFilename:completionHandler:"
  },
  "title" : "insertAttachment(_:withAlternateFilename:completionHandler:)"
}
-->

# insertAttachment(_:withAlternateFilename:completionHandler:)

Inserts an attachment into the current context.

```
func insertAttachment(_ URL: URL, withAlternateFilename filename: String?, completionHandler: (@Sendable ((any Error)?) -> Void)? = nil)
```

## Parameters

`URL`

A URL for the media file to be inserted. This URL must refer to a file saved on the device.

`filename`

An alternate name for the file. Use an alternate filename to better describe the attachment or to make the name more readable. If you pass a string, the Messages app uses the string as the attachment’s filename. If you pass `nil`, it parses the filename from the URL.

`completionHandler`

A block that is called as soon as the insertion is complete. This block is passed the following parameter:

- error: An error object. If an error occurred, this object contains information about the error; otherwise, it is set to `nil`.

## Discussion

> Important:
> You can call this method from synchronous code using a completion handler, as shown on this page, or you can call it as an asynchronous method that has the following declaration:
> 
> ```swift
> func insertAttachment(_ URL: URL, withAlternateFilename filename: String?) async throws
> ```
> 
> For information about concurrency and asynchronous code in Swift, see <doc://com.apple.documentation/documentation/Swift/calling-objective-c-apis-asynchronously>.

Use this method to insert a media attachment into the current context. For the [`MSMessagesAppPresentationContext.messages`](/documentation/Messages/MSMessagesAppPresentationContext/messages) context, this method inserts the attachment into the Message app’s input field. Users can then send the media file by tapping Send.

For the [`MSMessagesAppPresentationContext.media`](/documentation/Messages/MSMessagesAppPresentationContext/media) context, the method only supports images. It places the image in the Messages camera or FaceTime. If the attachment is not an image type supported by [`MSSticker`](/documentation/Messages/MSSticker), the method fails with an [`MSMessageErrorCode.apiUnavailableInPresentationContext`](/documentation/Messages/MSMessageErrorCode/apiUnavailableInPresentationContext) error.

This method operates asynchronously. Although the method returns immediately, the actual work is deferred and performed in the background. As soon as the attachment is inserted, the system calls the completion block on a background queue.

> Note:
> This method does not send the media file. It inserts the media file into the Messages app’s input field. The file is not sent until the user taps Send.

---

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)