---
title: "save(to:for:completionHandler:)"
framework: uikit
role: symbol
role_heading: Instance Method
path: "uikit/uidocument/save(to:for:completionhandler:)"
---

# save(to:for:completionHandler:)

Saves document data to the specified location in the application sandbox.

## Declaration

```swift
func save(to url: URL, for saveOperation: UIDocument.SaveOperation, completionHandler: (@Sendable (Bool) -> Void)? = nil)
```

```swift
func save(to url: URL, for saveOperation: UIDocument.SaveOperation) async -> Bool
```

## Parameters

- `url`: The file URL identifying the location in the application sandbox to write the document data to. Typically, this is the URL obtained from the doc://com.apple.uikit/documentation/UIKit/UIDocument/fileURL property.
- `saveOperation`: A constant that indicates whether the document file is being written the first time or whether it is being overwritten. See doc://com.apple.uikit/documentation/UIKit/UIDocument/SaveOperation for details.
- `completionHandler`: A block with code that is executed when the save operation concludes. The block returns no value and has one parameter: This block is invoked on the calling queue.

## Discussion

Discussion The default implementation of this method first calls the contents(forType:) method synchronously on the calling queue to get the document data to save. Then it calls the writeContents(_:andAttributes:safelyTo:for:) method on a background queue to perform the actual writing of the data to disk. If you override this method, it’s recommended that you first call the superclass implementation of the method (super). If you do not call super, you must do two things: Call performAsynchronousFileAccess(_:) to put the save operation on a background queue. In the block parameter, implement coordinated writing by using the NSFileCoordinator class. From within the coordinated write, call writeContents(_:andAttributes:safelyTo:for:).

## See Also

### Writing document data

- [close(completionHandler:)](uikit/uidocument/close(completionhandler:).md)
- [contents(forType:)](uikit/uidocument/contents(fortype:).md)
- [writeContents(_:andAttributes:safelyTo:for:)](uikit/uidocument/writecontents(_:andattributes:safelyto:for:).md)
- [writeContents(_:to:for:originalContentsURL:)](uikit/uidocument/writecontents(_:to:for:originalcontentsurl:).md)
- [savingFileType](uikit/uidocument/savingfiletype.md)
- [fileAttributesToWrite(to:for:)](uikit/uidocument/fileattributestowrite(to:for:).md)
- [fileNameExtension(forType:saveOperation:)](uikit/uidocument/filenameextension(fortype:saveoperation:).md)
