-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVaultVerification.swift
More file actions
178 lines (145 loc) · 6.05 KB
/
VaultVerification.swift
File metadata and controls
178 lines (145 loc) · 6.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//
// VaultVerification.swift
// Jot.
//
// Created by Suchit on 17/07/17.
// Copyright © 2017 Suchit. All rights reserved.
//
import UIKit
import LocalAuthentication
class VaultVerification: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let hasLogin = UserDefaults.standard.bool(forKey: "hasLoginKey")
// 2.
if hasLogin {
loginButton.setTitle("LOGIN", for: UIControlState.normal)
loginButton.tag = loginButtonTag
} else {
loginButton.setTitle("CREATE", for: UIControlState.normal)
loginButton.tag = createLoginButtonTag
}
// Do any additional setup after loading the view.
}
@IBAction func touchIdBtnPressed(_ sender: Any) {
let authenticationContext = LAContext()
var error: NSError?
if authenticationContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error:
&error){
authenticationContext.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: "Please Authenticate With Fingerprint To Continue", reply: { (success, error) in
if success{
self.navigateToAuthenticatedVC()
}else{
if let error = error as NSError?{
let message = self.errorMessageForLAErrorCode(errorCode: error.code)
self.showAlertViewAfterEvaluatingPolicyWithMessage(message: message)
}
}
})
}
else{
showAlertViewForNoBiometrics()
return
}
}
func errorMessageForLAErrorCode(errorCode: Int) -> String{
var message = ""
switch errorCode {
case LAError.appCancel.rawValue:
message = "Authentication was cancelled by the application"
case LAError.authenticationFailed.rawValue:
message = "The user failed to provide valid credentials"
case LAError.invalidContext.rawValue:
message = "The context is invalid"
case LAError.passcodeNotSet.rawValue:
message = "Passcode not set on the device"
case LAError.systemCancel.rawValue:
message = "Authentication was cancelled by the system"
case LAError.touchIDLockout.rawValue:
message = "Too many failed attempts"
case LAError.touchIDNotAvailable.rawValue:
message = "Touch ID is not available on this device"
case LAError.userFallback.rawValue:
message = "User chose to fallback"
default:
message = "Error Not Available"
}
return message
}
func showAlertViewAfterEvaluatingPolicyWithMessage(message: String){
if message != "Error Not Available"
{
showAlertWithTitle(title: "Error", message: message)
}
}
func navigateToAuthenticatedVC()
{
DispatchQueue.main.async {
self.performSegue(withIdentifier: "VaultVCVerified", sender: AnyObject.self)
}
}
func showAlertViewForNoBiometrics()
{
showAlertWithTitle(title: "Touch ID Not Available", message: "This device does not support Touch ID Authentication")
}
func showAlertWithTitle(title: String , message: String){
let alertVC = UIAlertController(title: title, message: message, preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alertVC.addAction(okAction)
DispatchQueue.main.async {
self.present(alertVC, animated: true, completion: nil)
}
}
func checkLogin(password: String ) -> Bool {
if password == MyKeychainWrapper.myObject(forKey: "v_Data") as? String{
return true
} else {
return false
}
}
@IBAction func backBtnPressed(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
let MyKeychainWrapper = KeychainWrapper()
let createLoginButtonTag = 0
let loginButtonTag = 1
@IBOutlet weak var loginButton: UIButton!
@IBOutlet weak var passwordTextField: UITextField!
@IBAction func loginAction(sender: AnyObject) {
// 1.
if (passwordTextField.text == "") {
let alertView = UIAlertController(title: "Login Issue",
message: "Please Enter Password" as String, preferredStyle:.alert)
let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alertView.addAction(okAction)
self.present(alertView, animated: true, completion: nil)
return
}
// 2.
passwordTextField.resignFirstResponder()
// 3.
if sender.tag == createLoginButtonTag {
// 5.
MyKeychainWrapper.mySetObject(passwordTextField.text, forKey:kSecValueData)
MyKeychainWrapper.writeToKeychain()
UserDefaults.standard.set(true, forKey: "hasLoginKey")
UserDefaults.standard.synchronize()
loginButton.tag = loginButtonTag
passwordTextField.text = ""
performSegue(withIdentifier: "VaultVCVerified", sender: self)
} else if sender.tag == loginButtonTag {
// 6.
if checkLogin(password: passwordTextField.text!) {
performSegue(withIdentifier: "VaultVCVerified", sender: self)
passwordTextField.text = ""
} else {
// 7.
let alertView = UIAlertController(title: "Login Issue",
message: "Wrong Password" as String, preferredStyle:.alert)
let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alertView.addAction(okAction)
self.present(alertView, animated: true, completion: nil)
}
}
}
}