Latest 5.0.0
License MIT
Platforms ios 8.0, requires ARC
Authors , ,


Build Status




A Swift implementation of passcode lock for iOS with TouchID authentication.

Originally created by @yankodimitrov, then forked by @velikanov, @erickyim. hope you’re doing well.



Version Swift Xcode Support
v5.0.0 5.0 xcode10.2 iOS 8.0
v4.2.0 4.2 xcode10 iOS 8.0
v4.0.0 4.0 xcode9 iOS 8.0
v3.0 3.0 xcode8 iOS 8.0




To integrate PasscodeLock into your Xcode project using CocoaPods, specify it in your Podfile:

source ''
platform :ios, '8.0'


target 'your_target_name'
pod 'ESPasscodeLock', '~> 4.2.0'

Then, run the following command:

pod install


Add the following line to your Cartfile

github "erickyim/SwiftPasscodeLock"


  • Create an implementation of the PasscodeRepositoryType protocol.
import UIKit
import PasscodeLock

class PasscodeRepository: PasscodeRepositoryType {

    var hasPasscode: Bool = true
    var passcode: [String]?

    func savePasscode(passcode: [String]) {}

    func deletePasscode() {}

  • Create an implementation of the PasscodeLockConfigurationType protocol and set your preferred passcode lock configuration options. If you set the maximumInccorectPasscodeAttempts to a number greather than zero, when user will reach that number of incorrect passcode attempts a notification with name PasscodeLockIncorrectPasscodeNotification will be posted on the default NSNotificationCenter.
import UIKit
import PasscodeLock

class PasscodeLockConfiguration: PasscodeLockConfigurationType {
    var touchIdReason: String?
    let repository: PasscodeRepositoryType
    var passcodeLength = 4 // Specify the required amount of passcode digits
    var isTouchIDAllowed = true // Enable Touch ID
    var shouldRequestTouchIDImmediately = true // Use Touch ID authentication immediately
    var shouldDisableTouchIDButton = true // Hides manual touchID activation button from enter code view
    var maximumInccorectPasscodeAttempts = 3 // Maximum incorrect passcode attempts
    var shouldDismissOnTooManyAttempts = true // When cancellation is available, dismiss code input view after too many wrong code attempts

    init(repository: PasscodeRepositoryType) {
        self.repository = repository

    init() {
        self.repository = PasscodeRepository() // The repository that was created earlier
  • Create an instance of the PasscodeLockPresenter class. Next inside your UIApplicationDelegate implementation call it to present the passcode in didFinishLaunchingWithOptions and applicationDidEnterBackground methods. The passcode lock will be presented only if your user has set a passcode.

  • Allow your users to set a passcode by presenting the PasscodeLockViewController in .SetPasscode state:
let configuration = ... // your implementation of the PasscodeLockConfigurationType protocol

let passcodeViewController = PasscodeLockViewController(state: .SetPasscode, configuration: configuration)

presentViewController(passcodeViewController, animated: true, completion: nil)

You can present the PasscodeLockViewController in one of the four initial states using the LockState enumeration options: .enterPasscode, .enterOptionalPasscode, .setPasscode, .changePasscode, .removePasscode.

Following callbacks are available:

    open var successCallback: ((_ lock: PasscodeLockType) -> Void)?
    open var dismissCompletionCallback: (()->Void)?
    open var cancelCompletionCallback: (()->Void)?
    open var wrongPasswordCallback: ((_ attemptNo: Int) -> Void)?
    open var tooManyAttemptsCallback: ((_ attemptNo: Int)->Void)?

Also you can set the initial passcode lock state to your own implementation of the PasscodeLockStateType protocol.


Custom Design

The PasscodeLock will look for PasscodeLockView.xib inside your app bundle and if it can’t find it will load its default one, so if you want to have a custom design create a new xib with the name PasscodeLockView and set its owner to an instance of PasscodeLockViewController class.

Keep in mind that when using custom classes that are defined in another module, you’ll need to set the Module field to that module’s name in the Identity Inspector:


Then connect the view outlet to the view of your xib file and make sure to conenct the remaining IBOutlets and IBActions.

PasscodeLock comes with two view components: PasscodeSignPlaceholderView and PasscodeSignButton that you can use to create your own custom designs. Both classes are @IBDesignable and @IBInspectable, so you can see their appearance and change their properties right inside the interface builder:



Take a look at PasscodeLock/en.lproj/PasscodeLock.strings for the localization keys. Here again the PasscodeLock will look for the PasscodeLock.strings file inside your app bundle and if it can’t find it will use the default localization file.

Demo App

The demo app comes with a simple implementation of the PasscodeRepositoryType protocol that is using the NSUserDefaults to store and retrieve the passcode. In your real applications you will probably want to use the Keychain API. Keep in mind that the Keychain records will not be removed when your user deletes your app.

Latest podspec

    "name": "ESPasscodeLock",
    "module_name": "PasscodeLock",
    "version": "5.0.0",
    "swift_version": "5.0",
    "platforms": {
        "ios": "8.0"
    "license": {
        "type": "MIT",
        "file": "LICENSE.txt"
    "summary": "An iOS passcode lock with Touch ID authentication written in Swift.",
    "homepage": "",
    "authors": {
        "Oskari Rauta": "",
        "Yanko Dimitrov": "",
        "Chris Ziogas": ""
    "source": {
        "git": "",
        "tag": "v5.0.0"
    "source_files": [
    "resources": [
    "ios": {
        "frameworks": "LocalAuthentication"
    "requires_arc": true

Pin It on Pinterest

Share This