<!--
{
  "availability" : [
    "iOS: 18.0.0 -",
    "iPadOS: 18.0.0 -",
    "macCatalyst: -",
    "macOS: 15.0.0 -",
    "tvOS: 18.0.0 -",
    "visionOS: 2.0.0 -",
    "watchOS: 11.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "AppIntents",
  "identifier" : "/documentation/AppIntents/IntentFile/withFile(contentType:allowOpenInPlace:fileHandler:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "App Intents",
      "AppIntents"
    ],
    "preciseIdentifier" : "s:10AppIntents10IntentFileV04withD011contentType16allowOpenInPlace11fileHandlerx07UniformG11Identifiers6UTTypeV_Sbx10Foundation3URLV_SbtYaKctYaKlF"
  },
  "title" : "withFile(contentType:allowOpenInPlace:fileHandler:)"
}
-->

# withFile(contentType:allowOpenInPlace:fileHandler:)

Requests an `IntentFile` representation as a file url.

```
func withFile<Result>(contentType: UTType, allowOpenInPlace: Bool = false, fileHandler: @escaping (URL, Bool) async throws -> Result) async throws -> Result
```

## Parameters

`contentType`

A content type of the returned data.

`allowOpenInPlace`

Whether the file should be opened in place, if possible.

`fileHandler`

A closure that accepts the file URL as a parameter.
The file is written to a temporary destination and removed
right after the closure returns.

## Discussion

If the file is not opened in place, the system removes it after the fileHandler closure returns.

```swift
let image = try await intentFile.withFile(contentType: .png) { url, _ in
    let data = try Data(contentsOf: url)
    return CGImage(pngData: data)
}
```

---

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)