sendAttachment(_:withAlternateFilename:completionHandler:)
Sends the media file specified by the given URL.
Declaration
func sendAttachment(_ URL: URL, withAlternateFilename filename: String?, completionHandler: (@Sendable ((any Error)?) -> Void)? = nil)func sendAttachment(_ URL: URL, withAlternateFilename filename: String?) async throwsParameters
- URL:
A URL for the media file to send. This URL must refer to a file saved on the device.
- filename:
An alternative name for the file. Use an alternative 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 message starts sending. 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. An error occurs if the user hasn’t recently interacted with your app.
Mentioned in
Discussion
This method starts sending the attachment automatically, without any additional user interactions. You can call this method only in response to a user action while in the MSMessagesAppPresentationContext.messages context.
When calling this method, the following rules apply:
If the app is not visible, the send fails with a MSMessageErrorCode.sendWhileNotVisible error code.
If the app has not registered a recent touch interaction from the user, the send fails with a MSMessageErrorCode.sendWithoutRecentInteraction error code.
If the app is in the MSMessagesAppPresentationContext.media context, the send fails with an 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 message starts to send, the system calls the completion block on a background queue.