<!--
{
  "availability" : [
    "iOS: 4.0.0 -",
    "iPadOS: 4.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.6.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/UserDefaults/url(forKey:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSUserDefaults(im)URLForKey:"
  },
  "title" : "url(forKey:)"
}
-->

# url(forKey:)

Returns the URL associated with the specified key.

```
func url(forKey defaultName: String) -> URL?
```

## Parameters

`defaultName`

The key to retrieve from the defaults database.

## Return Value

The URL associated with `defaultName`, or `nil` if the key isn’t present
in the defaults database.

## Discussion

This method uses the data for the specified key to create and return a URL type. If
the key is present but the method can’t use it to create a URL, this method returns
`nil`. If a file URL contains a tilde (~) character in its path, this method replaces
the tilde with an expanded path. If you saved a bookmark URL for the key previously,
use the [`URLByResolvingBookmarkData:options:relativeToURL:bookmarkDataIsStale:error:`](/documentation/Foundation/NSURL/URLByResolvingBookmarkData:options:relativeToURL:bookmarkDataIsStale:error:)
method to resolve the bookmark data and retrieve an equivalent file URL.

---

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)