---
title: "retrievePreferredCredentials(_:)"
framework: threadnetwork
role: symbol
role_heading: Instance Method
path: "threadnetwork/thclient/retrievepreferredcredentials(_:)"
---

# retrievePreferredCredentials(_:)

Requests Thread credentials for the preferred network.

## Declaration

```swift
func retrievePreferredCredentials(_ completion: @escaping @Sendable (THCredentials?, (any Error)?) -> Void)
```

```swift
func preferredCredentials() async throws -> THCredentials
```

## Parameters

- `completion`: The completion handler the framework calls when the credentials become available.

## Mentioned in

Managing Thread network credentials Configuring a Border Router

## Discussion

Discussion 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: func preferredCredentials() async throws -> THCredentials For information about concurrency and asynchronous code in Swift, see Calling Objective-C APIs Asynchronously. When you call this method, an alert appears asking for user permission to access credentials. Call the method as follows: func obtainPreferredCredentials() async -> (cred: THCredentials? , err: Error? ) {     let client = THClient()     var credential: THCredentials?     var err:Error?     do {         credential = try await client.preferredCredentials()     } catch {         err = error     }     return (credential, err) }

## See Also

### Retrieving Credentials

- [isPreferredNetworkAvailable(completion:)](threadnetwork/thclient/ispreferrednetworkavailable(completion:).md)
- [checkPreferredNetwork(forActiveOperationalDataset:completion:)](threadnetwork/thclient/checkpreferrednetwork(foractiveoperationaldataset:completion:).md)
- [retrieveCredentials(forBorderAgent:completion:)](threadnetwork/thclient/retrievecredentials(forborderagent:completion:).md)
- [retrieveCredentials(forExtendedPANID:completion:)](threadnetwork/thclient/retrievecredentials(forextendedpanid:completion:).md)
- [retrieveAllCredentials(_:)](threadnetwork/thclient/retrieveallcredentials(_:).md)
- [retrieveAllActiveCredentials(_:)](threadnetwork/thclient/retrieveallactivecredentials(_:).md)
