mirror of
https://github.com/vector-im/element-ios.git
synced 2024-09-29 07:42:40 +00:00
Update PinCodePreferences to include biometrics config
This commit is contained in:
parent
4ff794f752
commit
52ef548159
1 changed files with 58 additions and 0 deletions
|
@ -16,6 +16,7 @@ limitations under the License.
|
|||
|
||||
import Foundation
|
||||
import KeychainAccess
|
||||
import LocalAuthentication
|
||||
|
||||
/// Pin code preferences.
|
||||
@objcMembers
|
||||
|
@ -29,6 +30,7 @@ final class PinCodePreferences: NSObject {
|
|||
|
||||
private struct StoreKeys {
|
||||
static let pin: String = "pin"
|
||||
static let biometricsEnabled: String = "biometricsEnabled"
|
||||
}
|
||||
|
||||
static let shared = PinCodePreferences()
|
||||
|
@ -49,6 +51,10 @@ final class PinCodePreferences: NSObject {
|
|||
return RiotSettings.shared.forcePinProtection
|
||||
}
|
||||
|
||||
var isBiometricsAvailable: Bool {
|
||||
return LAContext().canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil)
|
||||
}
|
||||
|
||||
/// Allowed number of PIN trials before showing forgot help alert
|
||||
let allowedNumberOfTrialsBeforeAlert: Int = 5
|
||||
|
||||
|
@ -81,8 +87,60 @@ final class PinCodePreferences: NSObject {
|
|||
}
|
||||
}
|
||||
|
||||
var biometricsEnabled: Bool? {
|
||||
get {
|
||||
do {
|
||||
return try store.object(forKey: StoreKeys.biometricsEnabled) as? Bool
|
||||
} catch let error {
|
||||
NSLog("[PinCodePreferences] Error when reading biometrics enabled from store: \(error)")
|
||||
return nil
|
||||
}
|
||||
} set {
|
||||
do {
|
||||
try store.set(newValue, forKey: StoreKeys.biometricsEnabled)
|
||||
} catch let error {
|
||||
NSLog("[PinCodePreferences] Error when storing biometrics enabled to the store: \(error)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var isBiometricsSet: Bool {
|
||||
return biometricsEnabled == true
|
||||
}
|
||||
|
||||
func localizedBiometricsName() -> String? {
|
||||
if isBiometricsAvailable {
|
||||
let context = LAContext()
|
||||
switch context.biometryType {
|
||||
case .touchID:
|
||||
return VectorL10n.biometricsModeTouchId
|
||||
case .faceID:
|
||||
return VectorL10n.biometricsModeFaceId
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func biometricsIcon() -> UIImage? {
|
||||
if isBiometricsAvailable {
|
||||
let context = LAContext()
|
||||
switch context.biometryType {
|
||||
case .touchID:
|
||||
return Asset.Images.touchidIcon.image
|
||||
case .faceID:
|
||||
return Asset.Images.faceidIcon.image
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
/// Resets user PIN
|
||||
func reset() {
|
||||
pin = nil
|
||||
biometricsEnabled = nil
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue