banner



How To Repair And Update Keychains Un Xcode

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 information in the Keychain. Every bit yous probably noticed Apple'south keychain API is a bit verbose. This library was designed to provide shorter syntax for accomplishing a unproblematic task: reading/writing text values for specified keys:

              allow              keychain              =              KeychainSwift() keychain.prepare(                "hello world"              ,              forKey:                              "my cardinal"              ) keychain.get(                "my cardinal"              )

The Keychain library includes the following features:

  • Become, set and delete string, boolean and Information Keychain items
  • Specify item access security level
  • Synchronize items through iCloud
  • Share Keychain items with other apps

What's Keychain?

Keychain is a secure storage. You can store all kind of sensitive data in information technology: user passwords, credit bill of fare numbers, undercover tokens etc. Once stored in Keychain this information is only available to your app, other apps can't see it. Likewise that, operating arrangement makes certain this information is kept and processed deeply. For instance, text stored in Keychain tin not be extracted from iPhone backup or from its file organisation. Apple recommends storing only modest corporeality of information in the Keychain. If you need to secure something large you can encrypt it manually, save to a file and store the key in the Keychain.

Setup

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

Add source (iOS 7+)

But add together KeychainSwiftDistrib.swift file into your Xcode project.

Setup with Carthage (iOS 8+)

Alternatively, add together github "evgenyneu/keychain-swift" ~> xx.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 proper noun' pod 'KeychainSwift', '~> 20.0'                          

Setup with Swift Package Director

  • In Xcode select File > Add together Packages.
  • Enter this projection's URL: https://github.com/evgenyneu/keychain-swift.git

Legacy Swift versions

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

Usage

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

String values

              permit              keychain              =              KeychainSwift() keychain.set(                "hello world"              ,              forKey:                              "my key"              ) keychain.go(                "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 primal"              ) keychain.getData(                "my central"              )

Removing keys from Keychain

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

Return all keys

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

Advanced options

Keychain item access

Utilise withAccess parameter to specify the security level of the keychain storage. By default the .accessibleWhenUnlocked option is used. It is i of the virtually restrictive options and provides adept information protection.

              let keychain = KeychainSwift() keychain.set("Hullo globe", forKey: "central 1", withAccess: .accessibleWhenUnlocked)                          

You tin employ .accessibleAfterFirstUnlock if y'all need your app to access the keychain item while in the background. Annotation that it is less secure than the .accessibleWhenUnlocked option.

See the list of all available admission options.

Synchronizing keychain items with other devices

Ready synchronizable holding to true to enable keychain items synchronization across user'due south multiple devices. The synchronization will piece of work for users who take the "Keychain" enabled in the iCloud settings on their devices.

Setting synchronizable property to true will add the detail to other devices with the ready method and obtain synchronizable items with the go control. Deleting a synchronizable item volition remove information technology from all devices.

Note that you do Not demand to enable iCloud or Keychain Sharing capabilities in your app'south target for this characteristic to work.

                              //                First device                            let              keychain              =              KeychainSwift() keychain.synchronizable              =              true              keychain.set(                "how-do-you-do world"              ,              forKey:                              "my key"              )                              //                Second device                            let              keychain              =              KeychainSwift() keychain.synchronizable              =              true              keychain.get(                "my key"              )                              //                Returns "howdy world"            

Nosotros could not become the Keychain synchronization piece of work on macOS.

Sharing keychain items with other apps

In order to share keychain items between apps on the same device they demand to accept common Keychain Groups registered in Capabilities > Keychain Sharing settings. This tutorial shows how to ready it upward.

Apply accessGroup belongings to access shared keychain items. In the post-obit example we specify an access group "CS671JRA62.com.myapp.KeychainGroup" that volition be used to set up, get and delete an item "my key".

              permit              keychain              =              KeychainSwift() keychain.accessGroup              =                              "CS671JRA62.com.myapp.KeychainGroup"                                            //                Use your own access goup                            keychain.gear up(                "hi world"              ,              forKey:                              "my cardinal"              ) keychain.go(                "my key"              ) keychain.delete(                "my key"              ) keychain.articulate()

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

Setting cardinal prefix

One can pass a keyPrefix argument when initializing a KeychainSwift object. The string passed in keyPrefix statement will be used as a prefix to all the keys used in fix, get, getData and delete methods. Adding a prefix to the keychain keys can exist useful in unit tests. This prevents the tests from irresolute 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 up(                "hullo world"              ,              forKey:                              "hello"              )                              //                Value will be stored under "myTestKey_hello" key            

Bank check if operation was successful

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

              if              keychain.prepare(                "hello globe"              ,              forKey:                              "my cardinal"              ) {                              //                Keychain item is saved successfully              }              else              {                              //                Report error              }

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

keychain.set(                "hullo world"              ,              forKey:                              "my cardinal"              )              if              keychain.lastResultCode              !=              noErr {                              /*                Report fault                */                            }

Returning data every bit reference

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

              let              keychain              =              KeychainSwift() keychain.set(dataObject,              forKey:                              "my key"              ) keychain.getData(                "my fundamental"              ,              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 cypher instead of the stored Keychain value. It may be connected with the Keychain issue reported on Apple developer forums. The effect is random and hard to reproduce. If you experienced this problem experience complimentary to create an issue and share your story, so we can detect solutions.

Video tutorial

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

Keychain Swift video tutorial

Demo app

Keychain Swift demo app

Culling solutions

Hither are some other Keychain libraries.

  • DanielTomlinson/Latch
  • jrendel/SwiftKeychainWrapper
  • kishikawakatsumi/KeychainAccess
  • matthewpalmer/Locksmith
  • southward-aska/KeyClip
  • yankodimitrov/SwiftKeychain

Thanks 👍

  • The code is based on this example: https://gist.github.com/south-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 seven support.
  • Cheers to mikaoj for adding keychain synchronization.
  • Thank you to tcirwin for adding Swift 3.0 back up.
  • Thank you to Tulleb for calculation Xcode 8 beta half-dozen support.
  • Cheers to CraigSiemens for adding Swift 3.ane support.
  • Thanks to maxkramerbcgdv for fixing Package Managing director setup in Xcode eight.two.
  • Thank you to elikohen for fixing concurrency problems.
  • Thanks to beny for calculation Swift 4.ii support.
  • Thanks to xuaninbox for fixing watchOS deployment target for Xcode 10.
  • Thanks to schayes04 for adding Swift 5.0 support.
  • Thank you to mediym41 for adding ability to return data as reference.
  • Thanks to AnthonyOliveri for calculation 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 consequence, got stuck or just want to chat feel costless to create an issue. We will exist happy to help you.

License

Keychain Swift is released under the MIT License.

How To Repair And Update Keychains Un Xcode,

Source: https://github.com/evgenyneu/keychain-swift

Posted by: gardnerwastleime35.blogspot.com

0 Response to "How To Repair And Update Keychains Un Xcode"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel