velikanov / SwiftPasscodeLock

An iOS passcode lock with TouchID authentication written in Swift.
MIT License
199 stars 52 forks source link

deletePasscode() Not Getting Called. #12

Open AndyIbanez opened 8 years ago

AndyIbanez commented 8 years ago

Hey guys.

First, thanks for forking and reviving this project. While the documentation could use some improvement, I really like this component and it's a shame the original maintainer has basically abandoned the project.

But anyway, to the point:

Like the title says, deletePasscode() doesn't seem to be getting called at all. Here's some code:

It presents the view controller without any issues:

        let configuration = PasscodeLockConfiguration() 
                let passcodeViewController = PasscodeLockViewController(state: .RemovePasscode, configuration: configuration)
                presentViewController(passcodeViewController, animated: true, completion: nil)

The PasscodeLockConfiguration and PasscodeLockRepository look like this:

let MignoriUserAccount = "com.ios.fairese.Mignori.LocksmithUserAccount.Default"

struct PasscodeLockConfiguration: PasscodeLockConfigurationType {

    let repository: PasscodeRepositoryType
    let passcodeLength = 4
    var isTouchIDAllowed = Settings.lockScreenPasscodeSettingsSupportTouchID
    let shouldRequestTouchIDImmediately = true
    let maximumInccorectPasscodeAttempts = -1

    init(repository: PasscodeRepositoryType) {

        self.repository = repository
    }

    init() {
        self.repository = LocksmithPasscodeRepository()
    }
}

class LocksmithPasscodeRepository: PasscodeRepositoryType {

    var passcode: [String]? {
        return Locksmith.loadDataForUserAccount(MignoriUserAccount)?["passcode"] as? [String]
    }

    var hasPasscode: Bool {
        get {
            return Locksmith.loadDataForUserAccount(MignoriUserAccount) != nil
        }
    }

    func savePasscode(passcode: [String]) {
        do {
            Collector.logEvent("Passcode", withParameters: ["State" : "Enabled"])
            try Locksmith.deleteDataForUserAccount(MignoriUserAccount) // There is no "update password" method sooo yeah.
            try Locksmith.saveData(["passcode": passcode], forUserAccount: MignoriUserAccount)
        } catch let e {
            Collector.logEvent("Failed to Save Passcode", withParameters: ["message" : "\(e)"])
        }
    }

    func deletePasscode() {
        print("bro?")
        do {
            try Locksmith.deleteDataForUserAccount(MignoriUserAccount)
            Collector.logEvent("Passcode", withParameters: ["State" : "Disabled"])
        } catch let e {
            print("Wut err \(e)")
            Collector.logEvent("Failed to Remove Passcode", withParameters: ["message" : "\(e)"])
        }
    }
}

The print("bro?") line never gets executed, so let alone the rest of the lines inside the deletePasscode method. The other three methods get called with no issue, but for some reason deletePasscode doesn't get called, not even when the view controller is presented and I put in my passcode to remove it.

AndyIbanez commented 8 years ago

I checked the demo again and found my answer. I need to call it manually on the successCallback:

                passcodeViewController.successCallback = { lock in
                    lock.repository.deletePasscode()
                }

I will probably try to contribute a much better documentation for this project. It's great but really lacking in that area.

velikanov commented 8 years ago

:+1: @AndyIbanez please feel free to make a PR

ziogaschr commented 8 years ago

I would think that this is an inconsistency of the library and that the library will be better to handle it. Although it is hard to fix it and be backward compatible.

@AndyIbanez nice way to handle this for now 👍