<!--
{
  "availability" : [
    "iOS: 11.0.0 -",
    "iPadOS: 11.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.15.0 -",
    "tvOS: 11.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 9.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "DeviceCheck",
  "identifier" : "/documentation/DeviceCheck/DCDevice/generateToken(completionHandler:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "DeviceCheck"
    ],
    "preciseIdentifier" : "c:objc(cs)DCDevice(im)generateTokenWithCompletionHandler:"
  },
  "title" : "generateToken(completionHandler:)"
}
-->

# generateToken(completionHandler:)

Generates a token that identifies the current device.

```
func generateToken(completionHandler completion: @escaping @Sendable (Data?, (any Error)?) -> Void)
```

## Parameters

`completion`

A completion block that includes the following parameters:

- `token`:  An ephemeral token that identifies the current device.
- `error`: The error that occurred, if any.

## Discussion> Concurrency Note: 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 generateToken() async throws -> Data
> ```
> 
> For example:
> 
> ```swift
> let token = try await generateToken()
> ```
> 
> For information about concurrency and asynchronous code in Swift, see <doc://com.apple.documentation/documentation/Swift/calling-objective-c-apis-asynchronously>.

Your server uses the generated token in its requests to get or set the
persistent bits for the current device. You should treat the token you
receive in the completion block as single-use. Although the token remains
valid long enough for your server to retry a specific request if necessary,
you should not use a token multiple times. Instead, use this method to
generate a new token.

> Note: The app you use to generate the token must be associated with your
> developer account; otherwise, the generation request fails.

---

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)