8000 GitHub - codestruction-eu/keychain-swift: Helper functions for saving text in Keychain securely for iOS, OS X, tvOS and watchOS.
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Helper functions for saving text in Keychain securely for iOS, OS X, tvOS and watchOS.

License

Notifications You must be signed in to change notification settings

codestruction-eu/keychain-swift

< 8000 script type="application/json" data-target="react-partial.embeddedData">{"props":{"initialPayload":{"allShortcutsEnabled":false,"path":"/","repo":{"id":430137912,"defaultBranch":"master","name":"keychain-swift","ownerLogin":"codestruction-eu","currentUserCanPush":false,"isFork":true,"isEmpty":false,"createdAt":"2021-11-20T15:23:59.000Z","ownerAvatar":"https://avatars.githubusercontent.com/u/94748529?v=4","public":true,"private":false,"isOrgOwned":true},"currentUser":null,"refInfo":{"name":"master","listCacheKey":"v0:1716136566.0","canEdit":false,"refType":"branch","currentOid":"5e1b02b6a9dac2a759a1d5dbc175c86bd192a608"},"tree":{"items":[{"name":"Demo","path":"Demo","contentType":"directory"},{"name":"Distrib","path":"Distrib","contentType":"directory"},{"name":"KeychainSwift.xcodeproj","path":"KeychainSwift.xcodeproj","contentType":"directory"},{"name":"Sources","path":"Sources","contentType":"directory"},{"name":"Tests/KeychainSwiftTests","path":"Tests/KeychainSwiftTests","contentType":"directory","hasSimplifiedPath":true},{"name":"graphics","path":"graphics","contentType":"directory"},{"name":"macOS Demo","path":"macOS Demo","contentType":"directory"},{"name":"scripts","path":"scripts","contentType":"directory"},{"name":".gitignore","path":".gitignore","contentType":"file"},{"name":"CHANGELOG.md","path":"CHANGELOG.md","contentType":"file"},{"name":"CONTRIBUTING.md","path":"CONTRIBUTING.md","contentType":"file"},{"name":"ISSUE_TEMPLATE.md","path":"ISSUE_TEMPLATE.md","contentType":"file"},{"name":"KeychainSwift.podspec","path":"KeychainSwift.podspec","contentType":"file"},{"name":"LICENSE","path":"LICENSE","contentType":"file"},{"name":"Package.swift","path":"Package.swift","contentType":"file"},{"name":"README.md","path":"README.md","contentType":"file"}],"templateDirectorySuggestionUrl":null,"readme":null,"totalCount":16,"showBranchInfobar":true},"fileTree":null,"fileTreeProcessingTime":null,"foldersToFetch":[],"treeExpanded":false,"symbolsExpanded":false,"isOverview":true,"overview":{"banners":{"shouldRecommendReadme":false,"isPersonalRepo":false,"showUseActionBanner":false,"actionSlug":null,"actionId":null,"showProtectBranchBanner":false,"publishBannersInfo":{"dismissActionNoticePath":"/settings/dismiss-notice/publish_action_from_repo","releasePath":"/codestruction-eu/keychain-swift/releases/new?marketplace=true","showPublishActionBanner":false},"interactionLimitBanner":null,"showInvitationBanner":false,"inviterName":null,"actionsMigrationBannerInfo":{"releaseTags":[],"showImmutableActionsMigrationBanner":false,"initialMigrationStatus":null}},"codeButton":{"contactPath":"/contact","isEnterprise":false,"local":{"protocolInfo":{"httpAvailable":true,"sshAvailable":null,"httpUrl":"https://github.com/codestruction-eu/keychain-swift.git","showCloneWarning":null,"sshUrl":null,"sshCertificatesRequired":null,"sshCertificatesAvailable":null,"ghCliUrl":"gh repo clone codestruction-eu/keychain-swift","defaultProtocol":"http","newSshKeyUrl":"/settings/ssh/new","setProtocolPath":"/users/set_protocol"},"platformInfo":{"cloneUrl":"https://desktop.github.com","showVisualStudioCloneButton":false,"visualStudioCloneUrl":"https://windows.github.com","showXcodeCloneButton":false,"xcodeCloneUrl":"xcode://clone?repo=https%3A%2F%2Fgithub.com%2Fcodestruction-eu%2Fkeychain-swift","zipballUrl":"/codestruction-eu/keychain-swift/archive/refs/heads/master.zip"}},"newCodespacePath":"/codespaces/new?hide_repo_select=true\u0026repo=430137912"},"popovers":{"rename":null,"renamedParentRepo":null},"commitCount":"423","overviewFiles":[{"displayName":"README.md","repoName":"keychain-swift","refName":"master","path":"README.md","preferredFileType":"readme","tabName":"README","richText":"\u003carticle class=\"markdown-body entry-content container-lg\" itemprop=\"text\"\u003e\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch1 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eHelper functions for storing text in Keychain for iOS, macOS, tvOS and WatchOS\u003c/h1\u003e\u003ca id=\"user-content-helper-functions-for-storing-text-in-keychain-for-ios-macos-tvos-and-watchos\" class=\"anchor\" aria-label=\"Permalink: Helper functions for storing text in Keychain for iOS, macOS, tvOS and WatchOS\" href=\"#helper-functions-for-storing-text-in-keychain-for-ios-macos-tvos-and-watchos\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003e\u003ca href=\"https://github.com/Carthage/Carthage\"\u003e\u003cimg src=\"https://camo.githubusercontent.com/5cb89494518efc818a124fd801726287be9482423d1a161a6dec2066aa0f30db/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f43617274686167652d636f6d70617469626c652d3442433531442e7376673f7374796c653d666c6174\" alt=\"Carthage compatible\" data-canonical-src=\"https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat\" style=\"max-width: 100%;\"\u003e\u003c/a\u003e\n\u003ca href=\"http://cocoadocs.org/docsets/KeychainSwift\" rel=\"nofollow\"\u003e\u003cimg src=\"https://camo.githubusercontent.com/cf1a7c9208b22a4545544cc0de02152f1f85f29ce201ff09e3dfb96a01da3c8a/68747470733a2f2f696d672e736869656c64732e696f2f636f636f61706f64732f762f4b6579636861696e53776966742e7376673f7374796c653d666c6174\" alt=\"CocoaPods Version\" data-canonical-src=\"https://img.shields.io/cocoapods/v/KeychainSwift.svg?style=flat\" style=\"max-width: 100%;\"\u003e\u003c/a\u003e\n\u003ca href=\"https://github.com/apple/swift-package-manager\"\u003e\u003cimg src=\"https://camo.githubusercontent.com/b77f3cd38bd8253e01a3574b97af1b311e99d45fb88d87de49bba39b9f1dae0e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f53776966742532305061636b6167652532304d616e616765722d636f6d70617469626c652d627269676874677265656e2e737667\" alt=\"Swift Package Manager compatible\" data-canonical-src=\"https://img.shields.io/badge/Swift%20Package%20Manager-compatible-brightgreen.svg\" style=\"max-width: 100%;\"\u003e\u003c/a\u003e\n\u003ca href=\"http://cocoadocs.org/docsets/KeychainSwift\" rel=\"nofollow\"\u003e\u003cimg src=\"https://camo.githubusercontent.com/5a23fe215e8c983b665de0628c1188bea46e387af811a78fea21b7710d110a8a/68747470733a2f2f696d672e736869656c64732e696f2f636f636f61706f64732f6c2f4b6579636861696e53776966742e7376673f7374796c653d666c6174\" alt=\"License\" data-canonical-src=\"https://img.shields.io/cocoapods/l/KeychainSwift.svg?style=flat\" style=\"max-width: 100%;\"\u003e\u003c/a\u003e\n\u003ca href=\"http://cocoadocs.org/docsets/KeychainSwift\" rel=\"nofollow\"\u003e\u003cimg src=\"https://camo.githubusercontent.com/bdf094ef3ad168eaed7303a1176699c19aa9b0ad9ae41be5c12b0fe937a5c24c/68747470733a2f2f696d672e736869656c64732e696f2f636f636f61706f64732f702f4b6579636861696e53776966742e7376673f7374796c653d666c6174\" alt=\"Platform\" data-canonical-src=\"https://img.shields.io/cocoapods/p/KeychainSwift.svg?style=flat\" style=\"max-width: 100%;\"\u003e\u003c/a\u003e\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eThis is a collection of helper functions for saving text and data in the Keychain.\nAs you probably noticed Apple's keychain API is a bit verbose. This library was designed to provide shorter syntax for accomplishing a simple task: reading/writing text values for specified keys:\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-swift notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"let keychain = KeychainSwift()\nkeychain.set(\u0026quot;hello world\u0026quot;, forKey: \u0026quot;my key\u0026quot;)\nkeychain.get(\u0026quot;my key\u0026quot;)\"\u003e\u003cpre\u003e\u003cspan class=\"pl-k\"\u003elet\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ekeychain\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e \u003cspan class=\"pl-en\"\u003eKeychainSwift\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e(\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e)\u003c/span\u003e\nkeychain\u003cspan class=\"pl-kos\"\u003e.\u003c/span\u003e\u003cspan class=\"pl-en\"\u003eset\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e(\u003c/span\u003e\u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-s\"\u003ehello world\u003c/span\u003e\u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e,\u003c/span\u003e forKey\u003cspan class=\"pl-kos\"\u003e:\u003c/span\u003e \u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-s\"\u003emy key\u003c/span\u003e\u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e)\u003c/span\u003e\nkeychain\u003cspan class=\"pl-kos\"\u003e.\u003c/span\u003e\u003cspan class=\"pl-en\"\u003eget\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e(\u003c/span\u003e\u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-s\"\u003emy key\u003c/span\u003e\u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e)\u003c/span\u003e\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eThe Keychain library includes the following features:\u003c/p\u003e\n\u003cul dir=\"auto\"\u003e\n\u003cli\u003e\u003ca href=\"#usage\"\u003eGet, set and delete string, boolean and Data Keychain items\u003c/a\u003e\u003c/li\u003e\n\u003cli\u003e\u003ca href=\"#keychain_item_access\"\u003eSpecify item access security level\u003c/a\u003e\u003c/li\u003e\n\u003cli\u003e\u003ca href=\"#keychain_synchronization\"\u003eSynchronize items through iCloud\u003c/a\u003e\u003c/li\u003e\n\u003cli\u003e\u003ca href=\"#keychain_access_groups\"\u003eShare Keychain items with other apps\u003c/a\u003e\u003c/li\u003e\n\u003c/ul\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch2 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eWhat's Keychain?\u003c/h2\u003e\u003ca id=\"user-content-whats-keychain\" class=\"anchor\" aria-label=\"Permalink: What's Keychain?\" href=\"#whats-keychain\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eKeychain is a secure storage. You can store all kind of sensitive data in it: user passwords, credit card numbers, secret tokens etc. Once stored in Keychain this information is only available to your app, other apps can't see it. Besides that, operating system makes sure this information is kept and processed securely. For example, text stored in Keychain can not be extracted from iPhone backup or from its file system. Apple recommends storing only small amount of data in the Keychain. If you need to secure something big you can encrypt it manually, save to a file and store the key in the Keychain.\u003c/p\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch2 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eSetup\u003c/h2\u003e\u003ca id=\"user-content-setup\" class=\"anchor\" aria-label=\"Permalink: Setup\" href=\"#setup\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eThere are four ways you can add KeychainSwift to your project.\u003c/p\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch4 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eAdd source (iOS 7+)\u003c/h4\u003e\u003ca id=\"user-content-add-source-ios-7\" class=\"anchor\" aria-label=\"Permalink: Add source (iOS 7+)\" href=\"#add-source-ios-7\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eSimply add \u003ca href=\"https://github.com/evgenyneu/keychain-swift/blob/master/Distrib/KeychainSwiftDistrib.swift\"\u003eKeychainSwiftDistrib.swift\u003c/a\u003e file into your Xcode project.\u003c/p\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch4 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eSetup with Carthage (iOS 8+)\u003c/h4\u003e\u003ca id=\"user-content-setup-with-carthage-ios-8\" class=\"anchor\" aria-label=\"Permalink: Setup with Carthage (iOS 8+)\" href=\"#setup-with-carthage-ios-8\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eAlternatively, add \u003ccode\u003egithub \"evgenyneu/keychain-swift\" ~\u0026gt; 24.0\u003c/code\u003e to your Cartfile and run \u003ccode\u003ecarthage update\u003c/code\u003e.\u003c/p\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch4 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eSetup with CocoaPods (iOS 8+)\u003c/h4\u003e\u003ca id=\"user-content-setup-with-cocoapods-ios-8\" class=\"anchor\" aria-label=\"Permalink: Setup with CocoaPods (iOS 8+)\" href=\"#setup-with-cocoapods-ios-8\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eIf you are using CocoaPods add this text to your Podfile and run \u003ccode\u003epod install\u003c/code\u003e.\u003c/p\u003e\n\u003cdiv class=\"snippet-clipboard-content notranslate position-relative overflow-auto\" data-snippet-clipboard-copy-content=\"use_frameworks!\ntarget 'Your target name'\npod 'KeychainSwift', '~\u0026gt; 24.0'\"\u003e\u003cpre class=\"notranslate\"\u003e\u003ccode\u003euse_frameworks!\ntarget 'Your target name'\npod 'KeychainSwift', '~\u0026gt; 24.0'\n\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch4 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eSetup with Swift Package Manager (in project)\u003c/h4\u003e\u003ca id=\"user-content-setup-with-swift-package-manager-in-project\" class=\"anchor\" aria-label=\"Permalink: Setup with Swift Package Manager (in project)\" href=\"#setup-with-swift-package-manager-in-project\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cul dir=\"auto\"\u003e\n\u003cli\u003eIn Xcode select \u003cem\u003eFile \u0026gt; Add Packages\u003c/em\u003e.\u003c/li\u003e\n\u003cli\u003eEnter this project's URL: \u003ca href=\"https://github.com/evgenyneu/keychain-swift.git\"\u003ehttps://github.com/evgenyneu/keychain-swift.git\u003c/a\u003e\u003c/li\u003e\n\u003c/ul\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch4 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eSetup with Swift Package Manager (in Swift Package)\u003c/h4\u003e\u003ca id=\"user-content-setup-with-swift-package-manager-in-swift-package\" class=\"anchor\" aria-label=\"Permalink: Setup with Swift Package Manager (in Swift Package)\" href=\"#setup-with-swift-package-manager-in-swift-package\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eIf you're using KeychainSwift in a Swift package, make sure to specify a \u003ccode\u003ename\u003c/code\u003e. This is because SPM cannot automatically resolve a name for a package that has a different Target name in its \u003ccode\u003ePackage.swift\u003c/code\u003e (namely \u003ccode\u003eKeychainSwift\u003c/code\u003e) that differs from the repo link (\u003ccode\u003ekeychain-swift\u003c/code\u003e).\u003c/p\u003e\n\u003cdiv class=\"snippet-clipboard-content notranslate position-relative overflow-auto\" data-snippet-clipboard-copy-content=\".package(name: \u0026quot;KeychainSwift\u0026quot;, url: \u0026quot;https://github.com/evgenyneu/keychain-swift.git\u0026quot;, from: \u0026quot;24.0.0\u0026quot;)\"\u003e\u003cpre class=\"notranslate\"\u003e\u003ccode\u003e.package(name: \"KeychainSwift\", url: \"https://github.com/evgenyneu/keychain-swift.git\", from: \"24.0.0\")\n\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch2 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eLegacy Swift versions\u003c/h2\u003e\u003ca id=\"user-content-legacy-swift-versions\" class=\"anchor\" aria-label=\"Permalink: Legacy Swift versions\" href=\"#legacy-swift-versions\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eSetup a \u003ca href=\"https://github.com/evgenyneu/keychain-swift/wiki/Legacy-Swift-versions\"\u003eprevious version\u003c/a\u003e of the library if you use an older version of Swift.\u003c/p\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch2 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eUsage\u003c/h2\u003e\u003ca id=\"user-content-usage\" class=\"anchor\" aria-label=\"Permalink: Usage\" href=\"#usage\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eAdd \u003ccode\u003eimport KeychainSwift\u003c/code\u003e to your source code unless you used the file setup method.\u003c/p\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch4 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eString values\u003c/h4\u003e\u003ca id=\"user-content-string-values\" class=\"anchor\" aria-label=\"Permalink: String values\" href=\"#string-values\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cdiv class=\"highlight highlight-source-swift notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"let keychain = KeychainSwift()\nkeychain.set(\u0026quot;hello world\u0026quot;, forKey: \u0026quot;my key\u0026quot;)\nkeychain.get(\u0026quot;my key\u0026quot;)\"\u003e\u003cpre\u003e\u003cspan class=\"pl-k\"\u003elet\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ekeychain\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e \u003cspan class=\"pl-en\"\u003eKeychainSwift\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e(\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e)\u003c/span\u003e\nkeychain\u003cspan class=\"pl-kos\"\u003e.\u003c/span\u003e\u003cspan class=\"pl-en\"\u003eset\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e(\u003c/span\u003e\u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-s\"\u003ehello world\u003c/span\u003e\u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e,\u003c/span\u003e forKey\u003cspan class=\"pl-kos\"\u003e:\u003c/span\u003e \u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-s\"\u003emy key\u003c/span\u003e\u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e)\u003c/span\u003e\nkeychain\u003cspan class=\"pl-kos\"\u003e.\u003c/span\u003e\u003cspan class=\"pl-en\"\u003eget\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e(\u003c/span\u003e\u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-s\"\u003emy key\u003c/span\u003e\u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e)\u003c/span\u003e\u003c/pre\u003e\u003c/div\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch4 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eBoolean values\u003c/h4\u003e\u003ca id=\"user-content-boolean-values\" class=\"anchor\" aria-label=\"Permalink: Boolean values\" href=\"#boolean-values\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cdiv class=\"highlight highlight-source-swift notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"let keychain = KeychainSwift()\nkeychain.set(true, forKey: \u0026quot;my key\u0026quot;)\nkeychain.getBool(\u0026quot;my key\u0026quot;)\"\u003e\u003cpre\u003e\u003cspan class=\"pl-k\"\u003elet\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ekeychain\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e \u003cspan class=\"pl-en\"\u003eKeychainSwift\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e(\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e)\u003c/span\u003e\nkeychain\u003cspan class=\"pl-kos\"\u003e.\u003c/span\u003e\u003cspan class=\"pl-en\"\u003eset\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e(\u003c/span\u003e\u003cspan class=\"pl-c1\"\u003etrue\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e,\u003c/span\u003e forKey\u003cspan class=\"pl-kos\"\u003e:\u003c/span\u003e \u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-s\"\u003emy key\u003c/span\u003e\u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e)\u003c/span\u003e\nkeychain\u003cspan class=\"pl-kos\"\u003e.\u003c/span\u003e\u003cspan class=\"pl-en\"\u003egetBool\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e(\u003c/span\u003e\u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-s\"\u003emy key\u003c/span\u003e\u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e)\u003c/span\u003e\u003c/pre\u003e\u003c/div\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch4 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eData values\u003c/h4\u003e\u003ca id=\"user-content-data-values\" class=\"anchor\" aria-label=\"Permalink: Data values\" href=\"#data-values\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cdiv class=\"highlight highlight-source-swift notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"let keychain = KeychainSwift()\nkeychain.set(dataObject, forKey: \u0026quot;my key\u0026quot;)\nkeychain.getData(\u0026quot;my key\u0026quot;)\"\u003e\u003cpre\u003e\u003cspan class=\"pl-k\"\u003elet\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ekeychain\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e \u003cspan class=\"pl-en\"\u003eKeychainSwift\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e(\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e)\u003c/span\u003e\nkeychain\u003cspan class=\"pl-kos\"\u003e.\u003c/span\u003e\u003cspan class=\"pl-en\"\u003eset\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e(\u003c/span\u003edataObject\u003cspan class=\"pl-kos\"\u003e,\u003c/span\u003e forKey\u003cspan class=\"pl-kos\"\u003e:\u003c/span\u003e \u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-s\"\u003emy key\u003c/span\u003e\u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e)\u003c/span\u003e\nkeychain\u003cspan class=\"pl-kos\"\u003e.\u003c/span\u003e\u003cspan class=\"pl-en\"\u003egetData\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e(\u003c/span\u003e\u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-s\"\u003emy key\u003c/span\u003e\u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e)\u003c/span\u003e\u003c/pre\u003e\u003c/div\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch4 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eRemoving keys from Keychain\u003c/h4\u003e\u003ca id=\"user-content-removing-keys-from-keychain\" class=\"anchor\" aria-label=\"Permalink: Removing keys from Keychain\" href=\"#removing-keys-from-keychain\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cdiv class=\"highlight highlight-source-swift notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"keychain.delete(\u0026quot;my key\u0026quot;) // Remove single key\nkeychain.clear() // Delete everything from app's Keychain. Does not work on macOS.\"\u003e\u003cpre\u003ekeychain\u003cspan class=\"pl-kos\"\u003e.\u003c/span\u003e\u003cspan class=\"pl-en\"\u003edelete\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e(\u003c/span\u003e\u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003 8000 cspan class=\"pl-s\"\u003emy key\u003c/span\u003e\u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e)\u003c/span\u003e // Remove single key\nkeychain\u003cspan class=\"pl-kos\"\u003e.\u003c/span\u003e\u003cspan class=\"pl-en\"\u003eclear\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e(\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e)\u003c/span\u003e // Delete everything from app's Keychain. Does not work on macOS.\u003c/pre\u003e\u003c/div\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch4 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eReturn all keys\u003c/h4\u003e\u003ca id=\"user-content-return-all-keys\" class=\"anchor\" aria-label=\"Permalink: Return all keys\" href=\"#return-all-keys\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cdiv class=\"highlight highlight-source-swift notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"let keychain = KeychainSwift()\nkeychain.allKeys // Returns the names of all keys\"\u003e\u003cpre\u003e\u003cspan class=\"pl-k\"\u003elet\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ekeychain\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e \u003cspan class=\"pl-en\"\u003eKeychainSwift\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e(\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e)\u003c/span\u003e\nkeychain\u003cspan class=\"pl-kos\"\u003e.\u003c/span\u003eallKeys // Returns the names of all keys\u003c/pre\u003e\u003c/div\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch2 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eAdvanced options\u003c/h2\u003e\u003ca id=\"user-content-advanced-options\" class=\"anchor\" aria-label=\"Permalink: Advanced options\" href=\"#advanced-options\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch3 id=\"user-content-keychain_item_access\" tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eKeychain item access\u003c/h3\u003e\u003ca id=\"user-content-keychain-item-access\" class=\"anchor\" aria-label=\"Permalink: Keychain item access\" href=\"#keychain-item-access\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eUse \u003ccode\u003ewithAccess\u003c/code\u003e parameter to specify the security level of the keychain storage.\nBy default the \u003ccode\u003e.accessibleWhenUnlocked\u003c/code\u003e option is used. It is one of the most restrictive options and provides good data protection.\u003c/p\u003e\n\u003cdiv class=\"snippet-clipboard-content notranslate position-relative overflow-auto\" data-snippet-clipboard-copy-content=\"let keychain = KeychainSwift()\nkeychain.set(\u0026quot;Hello world\u0026quot;, forKey: \u0026quot;key 1\u0026quot;, withAccess: .accessibleWhenUnlocked)\"\u003e\u003cpre class=\"notranslate\"\u003e\u003ccode\u003elet keychain = KeychainSwift()\nkeychain.set(\"Hello world\", forKey: \"key 1\", withAccess: .accessibleWhenUnlocked)\n\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eYou can use \u003ccode\u003e.accessibleAfterFirstUnlock\u003c/code\u003e if you need your app to access the keychain item while in the background. Note that it is less secure than the \u003ccode\u003e.accessibleWhenUnlocked\u003c/code\u003e option.\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eSee the list of all available \u003ca href=\"https://github.com/evgenyneu/keychain-swift/blob/master/Sources/KeychainSwiftAccessOptions.swift\"\u003eaccess options\u003c/a\u003e.\u003c/p\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch3 id=\"user-content-keychain_synchronization\" tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eSynchronizing keychain items with other devices\u003c/h3\u003e\u003ca id=\"user-content-synchronizing-keychain-items-with-other-devices\" class=\"anchor\" aria-label=\"Permalink: Synchronizing keychain items with other devices\" href=\"#synchronizing-keychain-items-with-other-devices\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eSet \u003ccode\u003esynchronizable\u003c/code\u003e property to \u003ccode\u003etrue\u003c/code\u003e to enable keychain items synchronization across user's multiple devices. The synchronization will work for users who have the \"Keychain\" enabled in the iCloud settings on their devices.\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eSetting \u003ccode\u003esynchronizable\u003c/code\u003e property to \u003ccode\u003etrue\u003c/code\u003e will add the item to other devices with the \u003ccode\u003eset\u003c/code\u003e method and obtain synchronizable items with the \u003ccode\u003eget\u003c/code\u003e command. Deleting a synchronizable item will remove it from all devices.\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eNote that you do NOT need to enable iCloud or Keychain Sharing capabilities in your app's target for this feature to work.\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-swift notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"// First device\nlet keychain = KeychainSwift()\nkeychain.synchronizable = true\nkeychain.set(\u0026quot;hello world\u0026quot;, forKey: \u0026quot;my key\u0026quot;)\n\n// Second device\nlet keychain = KeychainSwift()\nkeychain.synchronizable = true\nkeychain.get(\u0026quot;my key\u0026quot;) // Returns \u0026quot;hello world\u0026quot;\"\u003e\u003cpre\u003e// First device\n\u003cspan class=\"pl-k\"\u003elet\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ekeychain\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e \u003cspan class=\"pl-en\"\u003eKeychainSwift\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e(\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e)\u003c/span\u003e\nkeychain\u003cspan class=\"pl-kos\"\u003e.\u003c/span\u003esynchronizable \u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003etrue\u003c/span\u003e\nkeychain\u003cspan class=\"pl-kos\"\u003e.\u003c/span\u003e\u003cspan class=\"pl-en\"\u003eset\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e(\u003c/span\u003e\u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-s\"\u003ehello world\u003c/span\u003e\u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e,\u003c/span\u003e forKey\u003cspan class=\"pl-kos\"\u003e:\u003c/span\u003e \u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-s\"\u003emy key\u003c/span\u003e\u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e)\u003c/span\u003e\n\n// Second device\n\u003cspan class=\"pl-k\"\u003elet\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ekeychain\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e \u003cspan class=\"pl-en\"\u003eKeychainSwift\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e(\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e)\u003c/span\u003e\nkeychain\u003cspan class=\"pl-kos\"\u003e.\u003c/span\u003esynchronizable \u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003etrue\u003c/span\u003e\nkeychain\u003cspan class=\"pl-kos\"\u003e.\u003c/span\u003e\u003cspan class=\"pl-en\"\u003eget\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e(\u003c/span\u003e\u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-s\"\u003emy key\u003c/span\u003e\u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e)\u003c/span\u003e // Returns \"hello world\"\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eWe could not get the Keychain synchronization work on macOS.\u003c/p\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch3 id=\"user-content-keychain_access_groups\" tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eSharing keychain items with other apps\u003c/h3\u003e\u003ca id=\"user-content-sharing-keychain-items-with-other-apps\" class=\"anchor\" aria-label=\"Permalink: Sharing keychain items with other apps\" href=\"#sharing-keychain-items-with-other-apps\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eIn order to share keychain items between apps on the same device they need to have common \u003cem\u003eKeychain Groups\u003c/em\u003e registered in \u003cem\u003eCapabilities \u0026gt; Keychain Sharing\u003c/em\u003e settings. \u003ca href=\"http://evgenii.com/blog/sharing-keychain-in-ios/\" rel=\"nofollow\"\u003eThis tutorial\u003c/a\u003e shows how to set it up.\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eUse \u003ccode\u003eaccessGroup\u003c/code\u003e property to access shared keychain items. In the following example we specify an access group \"CS671JRA62.com.myapp.KeychainGroup\" that will be used to set, get and delete an item \"my key\".\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-swift notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"let keychain = KeychainSwift()\nkeychain.accessGroup = \u0026quot;CS671JRA62.com.myapp.KeychainGroup\u0026quot; // Use your own access goup\n\nkeychain.set(\u0026quot;hello world\u0026quot;, forKey: \u0026quot;my key\u0026quot;)\nkeychain.get(\u0026quot;my key\u0026quot;)\nkeychain.delete(\u0026quot;my key\u0026quot;)\nkeychain.clear()\"\u003e\u003cpre\u003e\u003cspan class=\"pl-k\"\u003elet\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ekeychain\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e \u003cspan class=\"pl-en\"\u003eKeychainSwift\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e(\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e)\u003c/span\u003e\nkeychain\u003cspan class=\"pl-kos\"\u003e.\u003c/span\u003eaccessGroup \u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e \u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-s\"\u003eCS671JRA62.com.myapp.KeychainGroup\u003c/span\u003e\u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e // Use your own access goup\n\nkeychain\u003cspan class=\"pl-kos\"\u003e.\u003c/span\u003e\u003cspan class=\"pl-en\"\u003eset\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e(\u003c/span\u003e\u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-s\"\u003ehello world\u003c/span\u003e\u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e,\u003c/span\u003e forKey\u003cspan class=\"pl-kos\"\u003e:\u003c/span\u003e \u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-s\"\u003emy key\u003c/span\u003e\u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e)\u003c/span\u003e\nkeychain\u003cspan class=\"pl-kos\"\u003e.\u003c/span\u003e\u003cspan class=\"pl-en\"\u003eget\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e(\u003c/span\u003e\u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-s\"\u003emy key\u003c/span\u003e\u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e)\u003c/span\u003e\nkeychain\u003cspan class=\"pl-kos\"\u003e.\u003c/span\u003e\u003cspan class=\"pl-en\"\u003edelete\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e(\u003c/span\u003e\u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-s\"\u003emy key\u003c/span\u003e\u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e)\u003c/span\u003e\nkeychain\u003cspan class=\"pl-kos\"\u003e.\u003c/span\u003e\u003cspan class=\"pl-en\"\u003eclear\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e(\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e)\u003c/span\u003e\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003e\u003cem\u003eNote\u003c/em\u003e: there is no way of sharing a keychain item between the watchOS 2.0 and its paired device: \u003ca href=\"https://forums.developer.apple.com/thread/5938\" rel=\"nofollow\"\u003ehttps://forums.developer.apple.com/thread/5938\u003c/a\u003e\u003c/p\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch3 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eSetting key prefix\u003c/h3\u003e\u003ca id=\"user-content-setting-key-prefix\" class=\"anchor\" aria-label=\"Permalink: Setting key prefix\" href=\"#setting-key-prefix\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eOne can pass a \u003ccode\u003ekeyPrefix\u003c/code\u003e argument when initializing a \u003ccode\u003eKeychainSwift\u003c/code\u003e object. The string passed in \u003ccode\u003ekeyPrefix\u003c/code\u003e argument will be used as a prefix to \u003cstrong\u003eall the keys\u003c/strong\u003e used in \u003ccode\u003eset\u003c/code\u003e, \u003ccode\u003eget\u003c/code\u003e, \u003ccode\u003egetData\u003c/code\u003e and \u003ccode\u003edelete\u003c/code\u003e methods. Adding a prefix to the keychain keys can be useful in unit tests. This prevents the tests from changing the Keychain keys that are used when the app is launched manually.\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eNote that \u003ccode\u003eclear\u003c/code\u003e method still clears everything from the Keychain regardless of the prefix used.\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-swift notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"let keychain = KeychainSwift(keyPrefix: \u0026quot;myTestKey_\u0026quot;)\nkeychain.set(\u0026quot;hello world\u0026quot;, forKey: \u0026quot;hello\u0026quot;)\n// Value will be stored under \u0026quot;myTestKey_hello\u0026quot; key\"\u003e\u003cpre\u003e\u003cspan class=\"pl-k\"\u003elet\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ekeychain\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e \u003cspan class=\"pl-en\"\u003eKeychainSwift\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e(\u003c/span\u003ekeyPrefix\u003cspan class=\"pl-kos\"\u003e:\u003c/span\u003e \u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-s\"\u003emyTestKey_\u003c/span\u003e\u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e)\u003c/span\u003e\nkeychain\u003cspan class=\"pl-kos\"\u003e.\u003c/span\u003e\u003cspan class=\"pl-en\"\u003eset\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e(\u003c/span\u003e\u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-s\"\u003ehello world\u003c/span\u003e\u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e,\u003c/span\u003e forKey\u003cspan class=\"pl-kos\"\u003e:\u003c/span\u003e \u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-s\"\u003ehello\u003c/span\u003e\u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e)\u003c/span\u003e\n// Value will be stored under \"myTestKey_hello\" key\u003c/pre\u003e\u003c/div\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch3 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eCheck if operation was successful\u003c/h3\u003e\u003ca id=\"user-content-check-if-operation-was-successful\" class=\"anchor\" aria-label=\"Permalink: Check if operation was successful\" href=\"#check-if-operation-was-successful\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eOne can verify if \u003ccode\u003eset\u003c/code\u003e, \u003ccode\u003edelete\u003c/code\u003e and \u003ccode\u003eclear\u003c/code\u003e methods finished successfully by checking their return values. Those methods return \u003ccode\u003etrue\u003c/code\u003e on success and \u003ccode\u003efalse\u003c/code\u003e on error.\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-swift notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"if keychain.set(\u0026quot;hello world\u0026quot;, forKey: \u0026quot;my key\u0026quot;) {\n // Keychain item is saved successfully\n} else {\n // Report error\n}\"\u003e\u003cpre\u003e\u003cspan class=\"pl-k\"\u003eif\u003c/span\u003e keychain\u003cspan class=\"pl-kos\"\u003e.\u003c/span\u003e\u003cspan class=\"pl-en\"\u003eset\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e(\u003c/span\u003e\u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-s\"\u003ehello world\u003c/span\u003e\u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e,\u003c/span\u003e forKey\u003cspan class=\"pl-kos\"\u003e:\u003c/span\u003e \u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-s\"\u003emy key\u003c/span\u003e\u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e)\u003c/span\u003e \u003cspan class=\"pl-kos\"\u003e{\u003c/span\u003e\n // Keychain item is saved successfully\n\u003cspan class=\"pl-kos\"\u003e}\u003c/span\u003e \u003cspan class=\"pl-k\"\u003eelse\u003c/span\u003e \u003cspan class=\"pl-kos\"\u003e{\u003c/span\u003e\n // Report error\n\u003cspan class=\"pl-kos\"\u003e}\u003c/span\u003e\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eTo get a specific failure reason use the \u003ccode\u003elastResultCode\u003c/code\u003e property containing result code for the last operation. See \u003ca href=\"https://developer.apple.com/documentation/security/1542001-security_framework_result_codes\" rel=\"nofollow\"\u003eKeychain Result Codes\u003c/a\u003e.\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-swift notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"keychain.set(\u0026quot;hello world\u0026quot;, forKey: \u0026quot;my key\u0026quot;)\nif keychain.lastResultCode != noErr { /* Report error */ }\"\u003e\u003cpre\u003ekeychain\u003cspan class=\"pl-kos\"\u003e.\u003c/span\u003e\u003cspan class=\"pl-en\"\u003eset\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e(\u003c/span\u003e\u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-s\"\u003ehello world\u003c/span\u003e\u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e,\u003c/span\u003e forKey\u003cspan class=\"pl-kos\"\u003e:\u003c/span\u003e \u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-s\"\u003emy key\u003c/span\u003e\u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e)\u003c/span\u003e\n\u003cspan class=\"pl-k\"\u003eif\u003c/span\u003e keychain\u003cspan class=\"pl-kos\"\u003e.\u003c/span\u003elastResultCode \u003cspan class=\"pl-c1\"\u003e!=\u003c/span\u003e noErr \u003cspan class=\"pl-kos\"\u003e{\u003c/span\u003e /* Report error */ \u003cspan class=\"pl-kos\"\u003e}\u003c/span\u003e\u003c/pre\u003e\u003c/div\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch3 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eReturning data as reference\u003c/h3\u003e\u003ca id=\"user-content-returning-data-as-reference\" class=\"anchor\" aria-label=\"Permalink: Returning data as reference\" href=\"#returning-data-as-reference\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eUse the \u003ccode\u003easReference: true\u003c/code\u003e parameter to return the data as reference, which is needed for \u003ca href=\"https://developer.apple.com/documentation/networkextension/nevpnprotocol\" rel=\"nofollow\"\u003eNEVPNProtocol\u003c/a\u003e.\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-swift notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"let keychain = KeychainSwift()\nkeychain.set(dataObject, forKey: \u0026quot;my key\u0026quot;)\nkeychain.getData(\u0026quot;my key\u0026quot;, asReference: true)\"\u003e\u003cpre\u003e\u003cspan class=\"pl-k\"\u003elet\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ekeychain\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e \u003cspan class=\"pl-en\"\u003eKeychainSwift\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e(\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e)\u003c/span\u003e\nkeychain\u003cspan class=\"pl-kos\"\u003e.\u003c/span\u003e\u003cspan class=\"pl-en\"\u003eset\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e(\u003c/span\u003edataObject\u003cspan class=\"pl-kos\"\u003e,\u003c/span\u003e forKey\u003cspan class=\"pl-kos\"\u003e:\u003c/span\u003e \u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-s\"\u003emy key\u003c/span\u003e\u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e)\u003c/span\u003e\nkeychain\u003cspan class=\"pl-kos\"\u003e.\u003c/span\u003e\u003cspan class=\"pl-en\"\u003egetData\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e(\u003c/span\u003e\u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-s\"\u003emy key\u003c/span\u003e\u003cspan class=\"pl-s\"\u003e\"\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e,\u003c/span\u003e asReference\u003cspan class=\"pl-kos\"\u003e:\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003etrue\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e)\u003c/span\u003e\u003c/pre\u003e\u003c/div\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch2 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eUsing KeychainSwift from Objective-C\u003c/h2\u003e\u003ca id=\"user-content-using-keychainswift-from-objective-c\" class=\"anchor\" aria-label=\"Permalink: Using KeychainSwift from Objective-C\" href=\"#using-keychainswift-from-objective-c\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003e\u003ca href=\"https://github.com/evgenyneu/keychain-swift/wiki/Using-KeychainSwift-in-Objective-C-project\"\u003eThis manual\u003c/a\u003e describes how to use KeychainSwift in Objective-C apps.\u003c/p\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch2 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003e❗️Known critical issue - call to action❗️\u003c/h2\u003e\u003ca id=\"user-content-️known-critical-issue---call-to-action️\" class=\"anchor\" aria-label=\"Permalink: ❗️Known critical issue - call to action❗️\" href=\"#️known-critical-issue---call-to-action️\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eIt \u003ca href=\"https://github.com/evgenyneu/keychain-swift/issues/15\" data-hovercard-type=\"issue\" data-hovercard-url=\"/evgenyneu/keychain-swift/issues/15/hovercard\"\u003ehas been reported\u003c/a\u003e that the library sometimes returns \u003ccode\u003enil\u003c/code\u003e instead of the stored Keychain value. It may be connected with \u003ca href=\"https://forums.developer.apple.com/thread/4743\" rel=\"nofollow\"\u003ethe Keychain issue\u003c/a\u003e reported on Apple developer forums. The issue is random and hard to reproduce. If you experienced this problem feel free to create an issue and share your story, so we can find solutions.\u003c/p\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch2 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eVideo tutorial\u003c/h2\u003e\u003ca id=\"user-content-video-tutorial\" class=\"anchor\" aria-label=\"Permalink: Video tutorial\" href=\"#video-tutorial\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eThanks to Alex Nagy from \u003ca href=\"https://rebeloper.com/\" rel=\"nofollow\"\u003erebeloper.com\u003c/a\u003e for creating this two-part \u003ca href=\"https://www.youtube.com/watch?v=1R-VIzjD4yo\u0026amp;list=PL_csAAO9PQ8bLfPF7JsnF-t4q63WKZ9O9\" rel=\"nofollow\"\u003evideo tutorial\u003c/a\u003e.\u003c/p\u003e\n\u003cp dir=\"auto\"\u003e\u003ca href=\"https://www.youtube.com/watch?v=1R-VIzjD4yo\u0026amp;list=PL_csAAO9PQ8bLfPF7JsnF-t4q63WKZ9O9\" rel=\"nofollow\"\u003e\u003cimg src=\"/codestruction-eu/keychain-swift/raw/master/graphics/keychain_swift_video_tutorial.jpg\" width=\"800\" alt=\"Keychain Swift video tutorial\" style=\"max-width: 100%;\"\u003e\u003c/a\u003e\u003c/p\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch2 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eDemo app\u003c/h2\u003e\u003ca id=\"user-content-demo-app\" class=\"anchor\" aria-label=\"Permalink: Demo app\" href=\"#demo-app\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003e\u003ca target=\"_blank\" rel=\"noopener noreferrer nofollow\" href=\"https://raw.githubusercontent.com/evgenyneu/keychain-swift/master/graphics/keychain-s 8000 wift-demo-3.png\"\u003e\u003cimg src=\"https://raw.githubusercontent.com/evgenyneu/keychain-swift/master/graphics/keychain-swift-demo-3.png\" alt=\"Keychain Swift demo app\" width=\"320\" style=\"max-width: 100%;\"\u003e\u003c/a\u003e\u003c/p\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch2 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eAlternative solutions\u003c/h2\u003e\u003ca id=\"user-content-alternative-solutions\" class=\"anchor\" aria-label=\"Permalink: Alternative solutions\" href=\"#alternative-solutions\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eHere are some other Keychain libraries.\u003c/p\u003e\n\u003cul dir=\"auto\"\u003e\n\u003cli\u003e\u003ca href=\"https://github.com/DanielTomlinson/Latch\"\u003eDanielTomlinson/Latch\u003c/a\u003e\u003c/li\u003e\n\u003cli\u003e\u003ca href=\"https://github.com/jrendel/SwiftKeychainWrapper\"\u003ejrendel/SwiftKeychainWrapper\u003c/a\u003e\u003c/li\u003e\n\u003cli\u003e\u003ca href=\"https://github.com/kishikawakatsumi/KeychainAccess\"\u003ekishikawakatsumi/KeychainAccess\u003c/a\u003e\u003c/li\u003e\n\u003cli\u003e\u003ca href=\"https://github.com/matthewpalmer/Locksmith\"\u003ematthewpalmer/Locksmith\u003c/a\u003e\u003c/li\u003e\n\u003cli\u003e\u003ca href=\"https://github.com/s-aska/KeyClip\"\u003es-aska/KeyClip\u003c/a\u003e\u003c/li\u003e\n\u003cli\u003e\u003ca href=\"https://github.com/yankodimitrov/SwiftKeychain\"\u003eyankodimitrov/SwiftKeychain\u003c/a\u003e\u003c/li\u003e\n\u003c/ul\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch2 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eThanks 👍\u003c/h2\u003e\u003ca id=\"user-content-thanks-\" class=\"anchor\" aria-label=\"Permalink: Thanks 👍\" href=\"#thanks-\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cul dir=\"auto\"\u003e\n\u003cli\u003eThe code is based on this example: \u003ca href=\"https://gist.github.com/s-aska/e7ad24175fb7b04f78e7\"\u003ehttps://gist.github.com/s-aska/e7ad24175fb7b04f78e7\u003c/a\u003e\u003c/li\u003e\n\u003cli\u003eThanks to \u003ca href=\"https://github.com/diogoguimaraes\"\u003ediogoguimaraes\u003c/a\u003e for adding Swift Package Manager setup option.\u003c/li\u003e\n\u003cli\u003eThanks to \u003ca href=\"https://github.com/glyuck\"\u003eglyuck\u003c/a\u003e for taming booleans.\u003c/li\u003e\n\u003cli\u003eThanks to \u003ca href=\"https://github.com/pepibumur\"\u003epepibumur\u003c/a\u003e for adding macOS, watchOS and tvOS support.\u003c/li\u003e\n\u003cli\u003eThanks to \u003ca href=\"https://github.com/ezura\"\u003eezura\u003c/a\u003e for iOS 7 support.\u003c/li\u003e\n\u003cli\u003eThanks to \u003ca href=\"https://github.com/mikaoj\"\u003emikaoj\u003c/a\u003e for adding keychain synchronization.\u003c/li\u003e\n\u003cli\u003eThanks to \u003ca href=\"https://github.com/tcirwin\"\u003etcirwin\u003c/a\u003e for adding Swift 3.0 support.\u003c/li\u003e\n\u003cli\u003eThanks to \u003ca href=\"https://github.com/Tulleb\"\u003eTulleb\u003c/a\u003e for adding Xcode 8 beta 6 support.\u003c/li\u003e\n\u003cli\u003eThanks to \u003ca href=\"https://github.com/CraigSiemens\"\u003eCraigSiemens\u003c/a\u003e for adding Swift 3.1 support.\u003c/li\u003e\n\u003cli\u003eThanks to \u003ca href=\"https://github.com/maxkramerbcgdv\"\u003emaxkramerbcgdv\u003c/a\u003e for fixing Package Manager setup in Xcode 8.2.\u003c/li\u003e\n\u003cli\u003eThanks to \u003ca href=\"https://github.com/elikohen\"\u003eelikohen\u003c/a\u003e for fixing concurrency issues.\u003c/li\u003e\n\u003cli\u003eThanks to \u003ca href=\"https://github.com/beny\"\u003ebeny\u003c/a\u003e for adding Swift 4.2 support.\u003c/li\u003e\n\u003cli\u003eThanks to \u003ca href=\"https://github.com/xuaninbox\"\u003exuaninbox\u003c/a\u003e for fixing watchOS deployment target for Xcode 10.\u003c/li\u003e\n\u003cli\u003eThanks to \u003ca href=\"https://github.com/schayes04\"\u003eschayes04\u003c/a\u003e for adding Swift 5.0 support.\u003c/li\u003e\n\u003cli\u003eThanks to \u003ca href=\"https://github.com/mediym41\"\u003emediym41\u003c/a\u003e for adding ability to return data as reference.\u003c/li\u003e\n\u003cli\u003eThanks to \u003ca href=\"https://github.com/AnthonyOliveri\"\u003eAnthonyOliveri\u003c/a\u003e for adding ability to run unit tests from Swift Package Manager.\u003c/li\u003e\n\u003cli\u003eThanks to \u003ca href=\"https://github.com/philippec\"\u003ephilippec\u003c/a\u003e for removing deprecated access options.\u003c/li\u003e\n\u003cli\u003eThanks to \u003ca href=\"https://github.com/lucasmpaim\"\u003elucasmpaim\u003c/a\u003e for adding ability to return the names of all keys.\u003c/li\u003e\n\u003c/ul\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch2 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eFeedback is welcome\u003c/h2\u003e\u003ca id=\"user-content-feedback-is-welcome\" class=\"anchor\" aria-label=\"Permalink: Feedback is welcome\" href=\"#feedback-is-welcome\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eIf you notice any issue, got stuck or just want to chat feel free to create an issue. We will be happy to help you.\u003c/p\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch2 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eLicense\u003c/h2\u003e\u003ca id=\"user-content-license\" class=\"anchor\" aria-label=\"Permalink: License\" href=\"#license\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eKeychain Swift is released under the \u003ca href=\"/codestruction-eu/keychain-swift/blob/master/LICENSE\"\u003eMIT License\u003c/a\u003e.\u003c/p\u003e\n\u003c/article\u003e","loaded":true,"timedOut":false,"errorMessage":null,"headerInfo":{"toc":[{"level":1,"text":"Helper functions for storing text in Keychain for iOS, macOS, tvOS and WatchOS","anchor":"helper-functions-for-storing-text-in-keychain-for-ios-macos-tvos-and-watchos","htmlText":"Helper functions for storing text in Keychain for iOS, macOS, tvOS and WatchOS"},{"level":2,"text":"What's Keychain?","anchor":"whats-keychain","htmlText":"What's Keychain?"},{"level":2,"text":"Setup","anchor":"setup","htmlText":"Setup"},{"level":4,"text":"Add source (iOS 7+)","anchor":"add-source-ios-7","htmlText":"Add source (iOS 7+)"},{"level":4,"text":"Setup with Carthage (iOS 8+)","anchor":"setup-with-carthage-ios-8","htmlText":"Setup with Carthage (iOS 8+)"},{"level":4,"text":"Setup with CocoaPods (iOS 8+)","anchor":"setup-with-cocoapods-ios-8","htmlText":"Setup with CocoaPods (iOS 8+)"},{"level":4,"text":"Setup with Swift Package Manager (in project)","anchor":"setup-with-swift-package-manager-in-project","htmlText":"Setup with Swift Package Manager (in project)"},{"level":4,"text":"Setup with Swift Package Manager (in Swift Package)","anchor":"setup-with-swift-package-manager-in-swift-package","htmlText":"Setup with Swift Package Manager (in Swift Package)"},{"level":2,"text":"Legacy Swift versions","anchor":"legacy-swift-versions","htmlText":"Legacy Swift versions"},{"level":2,"text":"Usage","anchor":"usage","htmlText":"Usage"},{"level":4,"text":"String values","anchor":"string-values","htmlText":"String values"},{"level":4,"text":"Boolean values","anchor":"boolean-values","htmlText":"Boolean values"},{"level":4,"text":"Data values","anchor":"data-values","htmlText":"Data values"},{"level":4,"text":"Removing keys from Keychain","anchor":"removing-keys-from-keychain","htmlText":"Removing keys from Keychain"},{"level":4,"text":"Return all keys","anchor":"return-all-keys","htmlText":"Return all keys"},{"level":2,"text":"Advanced options","anchor":"advanced-options","htmlText":"Advanced options"},{"level":3,"text":"Keychain item access","anchor":"keychain-item-access","htmlText":"Keychain item access"},{"level":3,"text":"Synchronizing keychain items with other devices","anchor":"synchronizing-keychain-items-with-other-devices","htmlText":"Synchronizing keychain items with other devices"},{"level":3,"text":"Sharing keychain items with other apps","anchor":"sharing-keychain-items-with-other-apps","htmlText":"Sharing keychain items with other apps"},{"level":3,"text":"Setting key prefix","anchor":"setting-key-prefix","htmlText":"Setting key prefix"},{"level":3,"text":"Check if operation was successful","anchor":"check-if-operation-was-successful","htmlText":"Check if operation was successful"},{"level":3,"text":"Returning data as reference","anchor":"returning-data-as-reference","htmlText":"Returning data as reference"},{"level":2,"text":"Using KeychainSwift from Objective-C","anchor":"using-keychainswift-from-objective-c","htmlText":"Using KeychainSwift from Objective-C"},{"level":2,"text":"❗️Known critical issue - call to action❗️","anchor":"️known-critical-issue---call-to-action️","htmlText":"❗️Known critical issue - call to action❗️"},{"level":2,"text":"Video tutorial","anchor":"video-tutorial","htmlText":"Video tutorial"},{"level":2,"text":"Demo app","anchor":"demo-app","htmlText":"Demo app"},{"level":2,"text":"Alternative solutions","anchor":"alternative-solutions","htmlText":"Alternative solutions"},{"level":2,"text":"Thanks 👍","anchor":"thanks-","htmlText":"Thanks 👍"},{"level":2,"text":"Feedback is welcome","anchor":"feedback-is-welcome","htmlText":"Feedback is welcome"},{"level":2,"text":"License","anchor":"license","htmlText":"License"}],"siteNavLoginPath":"/login?return_to=https%3A%2F%2Fgithub.com%2Fcodestruction-eu%2Fkeychain-swift"}},{"displayName":"LICENSE","repoName":"keychain-swift","refName":"master","path":"LICENSE","preferredFileType":"license","tabName":"MIT","richText":null,"loaded":false,"timedOut":false,"errorMessage":null,"headerInfo":{"toc":null,"siteNavLoginPath":"/login?return_to=https%3A%2F%2Fgithub.com%2Fcodestruction-eu%2Fkeychain-swift"}}],"overviewFilesProcessingTime":0}},"appPayload":{"helpUrl":"https://docs.github.com","findFileWorkerPath":"/assets-cdn/worker/find-file-worker-7d7eb7c71814.js","findInFileWorkerPath":"/assets-cdn/worker/find-in-file-worker-4dde5cbad90b.js","githubDevUrl":null,"enabled_features":{"copilot_workspace":null,"code_nav_ui_events":false,"react_blob_overlay":false,"accessible_code_button":true,"github_models_repo_integration":false}}}}
 
 

Repository files navigation

Helper functions for storing text in Keychain for iOS, macOS, tvOS and WatchOS

Carthage compatible CocoaPods Version Swift Package Manager compatible License Platform

This is a collection of helper functions for saving text and data in the Keychain. As you probably noticed Apple's keychain API is a bit verbose. This library was designed to provide shorter syntax for accomplishing a simple task: reading/writing text values for specified keys:

let keychain = KeychainSwift()
keychain.set("hello world", forKey: "my key")
keychain.get("my key")

The Keychain library includes the following features:

What's Keychain?

Keychain is a secure storage. You can store all kind of sensitive data in it: user passwords, credit card numbers, secret tokens etc. Once stored in Keychain this information is only available to your app, other apps can't see it. Besides that, operating system makes sure this information is kept and processed securely. For example, text stored in Keychain can not be extracted from iPhone backup or from its file system. Apple recommends storing only small amount of data in the Keychain. If you need to secure something big you can encrypt it manually, save to a file and store the key in the Keychain.

Setup

There are four ways you can add KeychainSwift to your project.

Add source (iOS 7+)

Simply add KeychainSwiftDistrib.swift file into your Xcode project.

Setup with Carthage (iOS 8+)

Alternatively, add github "evgenyneu/keychain-swift" ~> 24.0 to your Cartfile and run carthage update.

Setup with CocoaPods (iOS 8+)

If you are using CocoaPods add this text to your Podfile and run pod install.

use_frameworks!
target 'Your target name'
pod 'KeychainSwift', '~> 24.0'

Setup with Swift Package Manager (in project)

Setup with Swift Package Manager (in Swift Package)

If you're using KeychainSwift in a Swift package, make sure to specify a name. This is because SPM cannot automatically resolve a name for a package that has a different Target name in its Package.swift (namely KeychainSwift) that differs from the repo link (keychain-swift).

.package(name: "KeychainSwift", url: "https://github.com/evgenyneu/keychain-swift.git", from: "24.0.0")

Legacy Swift versions

Setup a previous version of the library if you use an older version of Swift.

Usage

Add import KeychainSwift to your source code unless you used the file setup method.

String values

let keychain = KeychainSwift()
keychain.set("hello world", forKey: "my key")
keychain.get("my key")

Boolean values

let keychain = KeychainSwift()
keychain.set(true, forKey: "my key")
keychain.getBool("my key")

Data values

let keychain = KeychainSwift()
keychain.set(dataObject, forKey: "my key")
keychain.getData("my key")

Removing keys from Keychain

keychain.delete("my key") // Remove single key
keychain.clear() // Delete everything from app's Keychain. Does not work on macOS.

Return all keys

let keychain = KeychainSwift()
keychain.allKeys // Returns the names of all keys

Advanced options

Keychain item access

Use withAccess parameter to specify the security level of the keychain storage. By default the .accessibleWhenUnlocked option is used. It is one of the most restrictive options and provides good data protection.

let keychain = KeychainSwift()
keychain.set("Hello world", forKey: "key 1", withAccess: .accessibleWhenUnlocked)

You can use .accessibleAfterFirstUnlock if you need your app to access the keychain item while in the background. Note that it is less secure than the .accessibleWhenUnlocked option.

See the list of all available access options.

Synchronizing keychain items with other devices

Set synchronizable property to true to enable keychain items synchronization across user's multiple devices. The synchronization will work for users who have the "Keychain" enabled in the iCloud settings on their devices.

Setting synchronizable property to true will add the item to other devices with the set method and obtain synchronizable items with the get command. Deleting a synchronizable item will remove it from all devices.

Note that you do NOT need to enable iCloud or Keychain Sharing capabilities in your app's target for this feature to work.

// First device
let keychain = KeychainSwift()
keychain.synchronizable = true
keychain.set("hello world", forKey: "my key")

// Second device
let keychain = KeychainSwift()
keychain.synchronizable = true
keychain.get("my key") // Returns "hello world"

We could not get the Keychain synchronization work on macOS.

Sharing keychain items with other apps

In order to share keychain items between apps on the same device they need to have common Keychain Groups registered in Capabilities > Keychain Sharing settings. This tutorial shows how to set it up.

Use accessGroup property to access shared keychain items. In the following example we specify an access group "CS671JRA62.com.myapp.KeychainGroup" that will be used to set, get and delete an item "my key".

let keychain = KeychainSwift()
keychain.accessGroup = "CS671JRA62.com.myapp.KeychainGroup" // Use your own access goup

keychain.set("hello world", forKey: "my key")
keychain.get("my key")
keychain.delete("my key")
keychain.clear()

Note: there is no way of sharing a keychain item between the watchOS 2.0 and its paired device: https://forums.developer.apple.com/thread/5938

Setting key prefix

One can pass a keyPrefix argument when initializing a KeychainSwift object. The string passed in keyPrefix argument will be used as a prefix to all the keys used in set, get, getData and delete methods. Adding a prefix to the keychain keys can be useful in unit tests. This prevents the tests from changing the Keychain keys that are used when the app is launched manually.

Note that clear method still clears everything from the Keychain regardless of the prefix used.

let keychain = KeychainSwift(keyPrefix: "myTestKey_")
keychain.set("hello world", forKey: "hello")
// Value will be stored under "myTestKey_hello" key

Check if operation was successful

One can verify if set, delete and clear methods finished successfully by checking their return values. Those methods return true on success and false on error.

if keychain.set("hello world", forKey: "my key") {
  // Keychain item is saved successfully
} else {
  // Report error
}

To get a specific failure reason use the lastResultCode property containing result code for the last operation. See Keychain Result Codes.

keychain.set("hello world", forKey: "my key")
if keychain.lastResultCode != noErr { /* Report error */ }

Returning data as reference

Use the asReference: true parameter to return the data as reference, which is needed for NEVPNProtocol.

let keychain = KeychainSwift()
keychain.set(dataObject, forKey: "my key")
keychain.getData("my key", asReference: true)

Using KeychainSwift from Objective-C

This manual describes how to use KeychainSwift in Objective-C apps.

❗️Known critical issue - call to action❗️

It has been reported that the library sometimes returns nil instead of the stored Keychain value. It may be connected with the Keychain issue reported on Apple developer forums. The issue is random and hard to reproduce. If you experienced this problem feel free to create an issue and share your story, so we can find solutions.

Video tutorial

Thanks to Alex Nagy from rebeloper.com for creating this two-part video tutorial.

Keychain Swift video tutorial

Demo app

Keychain Swift demo app

Alternative solutions

Here are some other Keychain libraries.

Thanks 👍

  • The code is based on this example: https://gist.github.com/s-aska/e7ad24175fb7b04f78e7
  • Thanks to diogoguimaraes for adding Swift Package Manager setup option.
  • Thanks to glyuck for taming booleans.
  • Thanks to pepibumur for adding macOS, watchOS and tvOS support.
  • Thanks to ezura for iOS 7 support.
  • Thanks to mikaoj for adding keychain synchronization.
  • Thanks to tcirwin for adding Swift 3.0 support.
  • Thanks to Tulleb for adding Xcode 8 beta 6 support.
  • Thanks to CraigSiemens for adding Swift 3.1 support.
  • Thanks to maxkramerbcgdv for fixing Package Manager setup in Xcode 8.2.
  • Thanks to elikohen for fixing concurrency issues.
  • Thanks to beny for adding Swift 4.2 support.
  • Thanks to xuaninbox for fixing watchOS deployment target for Xcode 10.
  • Thanks to schayes04 for adding Swift 5.0 support.
  • Thanks to mediym41 for adding ability to return data as reference.
  • Thanks to AnthonyOliveri for adding ability to run unit tests from Swift Package Manager.
  • Thanks to philippec for removing deprecated access options.
  • Thanks to lucasmpaim for adding ability to return the names of all keys.

Feedback is welcome

If you notice any issue, got stuck or just want to chat feel free to create an issue. We will be happy to help you.

License

Keychain Swift is released under the MIT License.

About

Helper functions for saving text in Keychain securely for iOS, OS X, tvOS and watchOS.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 93.4%
  • Shell 3.7%
  • Ruby 2.0%
  • Other 0.9%
0