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

# createDirectory(withName:inParentItemIdentifier:completionHandler:)

Creates a directory with the given name inside the given parent directory.

```
func createDirectory(withName directoryName: String, inParentItemIdentifier parentItemIdentifier: NSFileProviderItemIdentifier, completionHandler: @escaping @Sendable (NSFileProviderItem?, (any Error)?) -> Void)
```

```
func createDirectory(withName directoryName: String, inParentItemIdentifier parentItemIdentifier: NSFileProviderItemIdentifier) async throws -> NSFileProviderItem
```

## Parameters

`directoryName`

The name of the directory to be created.

`parentItemIdentifier`

The persistent identifier for the parent directory.

`completionHandler`

A block that takes the following parameters:

- `createdDirectoryItem`: A provider item that describes the newly created directory, 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 createDirectory(withName directoryName: String, inParentItemIdentifier 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 creates a directory. Override this method to create the directory locally. 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 `createdDirectoryItem` 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 value passed to the `directoryName` parameter.
- [`creationDate`](/documentation/FileProvider/NSFileProviderItemProtocol/creationDate): Set to the current date and time.
- [`typeIdentifier`](/documentation/FileProvider/NSFileProviderItemProtocol/typeIdentifier): Set to `public.folder`.
- [`childItemCount`](/documentation/FileProvider/NSFileProviderItemProtocol/childItemCount): Set to `0`.
- [`capabilities`](/documentation/FileProvider/NSFileProviderItemProtocol/capabilities): Set to define the actions that the user can perform on the directory (for example, [`allowsAddingSubItems`](/documentation/FileProvider/NSFileProviderItemCapabilities/allowsAddingSubItems), [`allowsReading`](/documentation/FileProvider/NSFileProviderItemCapabilities/allowsReading), and [`allowsWriting`](/documentation/FileProvider/NSFileProviderItemCapabilities/allowsWriting)).

The user’s ability to create 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)