<!--
{
  "availability" : [
    "iOS: 11.0.0 -",
    "iPadOS: 11.0.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "FileProvider",
  "identifier" : "/documentation/FileProvider/NSFileProviderExtension/importDocument(at:toParentItemIdentifier:completionHandler:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "File Provider",
      "FileProvider"
    ],
    "preciseIdentifier" : "c:objc(cs)NSFileProviderExtension(im)importDocumentAtURL:toParentItemIdentifier:completionHandler:"
  },
  "title" : "importDocument(at:toParentItemIdentifier:completionHandler:)"
}
-->

# importDocument(at:toParentItemIdentifier:completionHandler:)

Imports a file or package into the given parent directory.

```
func importDocument(at fileURL: URL, toParentItemIdentifier parentItemIdentifier: NSFileProviderItemIdentifier, completionHandler: @escaping @Sendable (NSFileProviderItem?, (any Error)?) -> Void)
```

## Parameters

`fileURL`

A security-scoped URL for the file to import. Call <doc://com.apple.documentation/documentation/Foundation/URL/startAccessingSecurityScopedResource()> on the URL before accessing it and  <doc://com.apple.documentation/documentation/Foundation/URL/stopAccessingSecurityScopedResource()> when finished.

`parentItemIdentifier`

The persistent identifier for the directory where the item will be imported.

`completionHandler`

A block that takes the following parameters:

- `importedDocumentItem`: A provider item that describes the newly imported item, or `nil` if an error occurred.
- `error`: An error object. If an error occurs, pass in an object that describes the error; otherwise, set it 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 importDocument(at fileURL: URL, toParentItemIdentifier parentItemIdentifier: NSFileProviderItemIdentifier) async throws -> NSFileProviderItem
> ```
> 
> For information about concurrency and asynchronous code in Swift, see <doc://com.apple.documentation/documentation/Swift/calling-objective-c-apis-asynchronously>.

This method is called when the user imports a document or directory. Override this method to move the file at the provided security-scoped URL to the file provider’s storage. Your implementation should return immediately. Call the completion handler before performing any network activity or other long-running tasks. Defer these tasks to the background.

The `importedDocumentItem` instance that you pass to the completion handler must define the following properties:

- [`itemIdentifier`](/documentation/FileProvider/NSFileProviderItemProtocol/itemIdentifier): This identifier may be temporary. If you later receive a permanent identifier from your server, delete the temporary item and add the permanent one.
- [`parentItemIdentifier`](/documentation/FileProvider/NSFileProviderItemProtocol/parentItemIdentifier): Set to the value passed to the `parentItemIdentifier` parameter.
- [`filename`](/documentation/FileProvider/NSFileProviderItemProtocol/filename): Set to the `fileURL` parameter’s  <doc://com.apple.documentation/documentation/Foundation/URLResourceKey/nameKey> resource value.
- [`creationDate`](/documentation/FileProvider/NSFileProviderItemProtocol/creationDate): Set to the `fileURL` parameter’s  <doc://com.apple.documentation/documentation/Foundation/URLResourceKey/creationDateKey> resource value.
- [`contentModificationDate`](/documentation/FileProvider/NSFileProviderItemProtocol/contentModificationDate): Set to the `fileURL` parameter’s  <doc://com.apple.documentation/documentation/Foundation/URLResourceKey/contentModificationDateKey> resource value.
- [`typeIdentifier`](/documentation/FileProvider/NSFileProviderItemProtocol/typeIdentifier): Set to the `fileURL` parameter’s  <doc://com.apple.documentation/documentation/Foundation/URLResourceKey/typeIdentifierKey> resource value.
- [`documentSize`](/documentation/FileProvider/NSFileProviderItemProtocol/documentSize): For a flat file, set to the `fileURL` parameter’s <doc://com.apple.documentation/documentation/Foundation/URLResourceKey/totalFileSizeKey> resource value. For a package, set to the sum of the contents’ file sizes.
- [`capabilities`](/documentation/FileProvider/NSFileProviderItemProtocol/capabilities): Set to define the actions that the user can perform on the directory (for example, [`allowsReading`](/documentation/FileProvider/NSFileProviderItemCapabilities/allowsReading) and [`allowsWriting`](/documentation/FileProvider/NSFileProviderItemCapabilities/allowsWriting)).

The user’s ability to import an item into a directory is controlled by the parent directory’s [`allowsAddingSubItems`](/documentation/FileProvider/NSFileProviderItemCapabilities/allowsAddingSubItems) capability.

---

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)