Instance Method


Writes the document data to disk at the sandbox location indicated by a file URL.


- (BOOL)writeContents:(id)contents toURL:(NSURL *)url forSaveOperation:(UIDocumentSaveOperation)saveOperation originalContentsURL:(NSURL *)originalContentsURL error:(NSError * _Nullable *)outError;



The document data to write to disk. Typically, the data is encapsulated by an NSData object (if a flat file) or an NSFileWrapper object (if a file package).

If the object encapsulating the document data is of some other type, you should override this method or writeContents:andAttributes:safelyToURL:forSaveOperation:error: to perform the actual writing of the data.


A file URL specifying the location of the document file in the application sandbox.


A constant that indicates whether the document file is being written the first time or whether it is being overwritten. See UIDocumentSaveOperation for details.


A file URL specifying the previous location of the document file (if not nil).


If you override this method and cannot write the document data for any reason, return by indirection an NSError object that encapsulates the reasons why you can’t. Otherwise, ignore this parameter.

Return Value

YES if the write operation succeeds, otherwise NO.


This method is called by the writeContents:andAttributes:safelyToURL:forSaveOperation:error: to write the actual file data. It is passed the contents object returned from your contentsForType:error: implementation. The default implementation of this method supports NSData or NSFileWrapper contents by asking the contents object to save itself to the corresponding URL.

If you override this method, you may choose to return a different type of data from contentsForType:error: or you may choose to not override contentsForType:error: and generate the writable data directly within this method. If you override this method, you should not invoke the superclass implementation.

See Also

Writing Document Data

- closeWithCompletionHandler:

Asynchronously closes the document after saving any changes.

- contentsForType:error:

Override this method to return the document data to be saved.

- saveToURL:forSaveOperation:completionHandler:

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

- writeContents:andAttributes:safelyToURL:forSaveOperation:error:

Ensures that document data is written safely to a specified location in the application sandbox.


Returns the file type to use for saving a document.

- fileAttributesToWriteToURL:forSaveOperation:error:

Returns a dictionary of file attributes to associate with the document file when writing or updating it.

- fileNameExtensionForType:saveOperation:

Returns a file extension to append to the file URL of the document file being written.