Instance Method


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


- (BOOL)writeContents:(id)contents andAttributes:(NSDictionary *)additionalFileAttributes safelyToURL:(NSURL *)url forSaveOperation:(UIDocumentSaveOperation)saveOperation 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:toURL:forSaveOperation:originalContentsURL:error: to perform the actual writing of the data.


A dictionary of NSFileManager file attributes to assign to the document file. The default implementation obtains these file attributes by calling fileAttributesToWriteToURL:forSaveOperation:error:.


The 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.


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 saveToURL:forSaveOperation:completionHandler: method to save the file data (and associated attributes in the case of an NSFileWrapper). It creates temporary files and directories as necessary so that successful saves can be completed atomically and failed saves can be rolled back cleanly. This method calls writeContents:toURL:forSaveOperation:originalContentsURL:error: to save the contents object, passing the location for the new saved file in the toURL parameter and the location of the previously existing file in the originalContentsURL parameter, if this is an overwrite operation.

If you want to change how file data is saved, you generally override the writeContents:toURL:forSaveOperation:originalContentsURL:error: method instead of this method. Additionally, you don’t need to call this method directly unless you are overriding the saveToURL:forSaveOperation:completionHandler: method.

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:toURL:forSaveOperation:originalContentsURL:error:

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


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.