-
Notifications
You must be signed in to change notification settings - Fork 1
/
ViewController.swift
199 lines (157 loc) · 8.02 KB
/
ViewController.swift
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//
// ViewController.swift
// SecretSwift
//
// Created by Julian Moorhouse on 29/08/2019.
// Copyright © 2019 Mindwarp Consultancy Ltd. All rights reserved.
//
import LocalAuthentication
import UIKit
class ViewController: UIViewController {
let KEY_SECRET_MESSAGE = "SecretMessage"
let KEY_PASSWORD = "Password"
@IBOutlet var secret: UITextView!
var doneButton: UIBarButtonItem!
var setPasswordButton: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
title = "Nothing to see here"
let notificationCenter = NotificationCenter.default
notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIResponder.keyboardWillHideNotification, object: nil)
notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
// when leave app
notificationCenter.addObserver(self, selector: #selector(saveSecretMessage), name: UIApplication.willResignActiveNotification, object: nil)
doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(saveSecretMessage))
setPasswordButton = UIBarButtonItem(barButtonSystemItem: .organize, target: self, action: #selector(setPassword))
shouldShowSettings()
}
@IBAction func authenticateTapped(_ sender: Any) {
checkBiometry({
// found
let context = LAContext()
let reason = "Identifiy yourself!" // for touch id
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) { [weak self] success, _ in
DispatchQueue.main.async {
if success {
self?.unlockSecretMessage()
} else {
let ac = UIAlertController(title: "Authentication failed", message: "You could not be verified. Please try again.", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
self?.present(ac, animated: true)
}
}
}
}) {
// not found
let ac = UIAlertController(title: "App Pasword", message: "Please provide your password!", preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default) {
[weak self, weak ac] _ in
guard let previousPassword = ac?.textFields?[0].text else { return }
if let text = KeychainWrapper.standard.string(forKey: self!.KEY_PASSWORD) {
if previousPassword == text {
self?.unlockSecretMessage()
} else {
let ac2 = UIAlertController(title: "App Pasword", message: "Password incorrect!", preferredStyle: .alert)
ac2.addAction(UIAlertAction(title: "OK", style: .default))
self?.present(ac2, animated: true)
}
}
}
ac.addAction(okAction)
ac.addTextField(configurationHandler: { textField in
textField.placeholder = "Password"
textField.isSecureTextEntry = true
})
ac.addAction(UIAlertAction(title: "Cancel", style: .cancel))
present(ac, animated: true)
}
}
@objc func adjustForKeyboard(notification: Notification) {
guard let keyboardValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else { return }
let keyboardScreenEnd = keyboardValue.cgRectValue
let keyboardViewEndFrame = view.convert(keyboardScreenEnd, from: view.window)
if notification.name == UIResponder.keyboardWillHideNotification {
secret.contentInset = .zero
} else {
secret.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardViewEndFrame.height - view.safeAreaInsets.bottom, right: 0)
}
secret.scrollIndicatorInsets = secret.contentInset
let selectedRange = secret.selectedRange
secret.scrollRangeToVisible(selectedRange)
}
func unlockSecretMessage() {
secret.isHidden = false
navigationItem.rightBarButtonItem = doneButton
navigationItem.leftBarButtonItem = setPasswordButton
title = "Secret stuff!"
if let text = KeychainWrapper.standard.string(forKey: KEY_SECRET_MESSAGE) {
secret.text = text
}
}
@objc func saveSecretMessage() {
guard secret.isHidden == false else { return }
KeychainWrapper.standard.set(secret.text, forKey: KEY_SECRET_MESSAGE)
secret.resignFirstResponder()
secret.isHidden = true
navigationItem.rightBarButtonItem = nil
shouldShowSettings()
title = "Nothing to see here"
}
func checkBiometry(_ foundCompletion: () -> (), _ notFoundCompletion: () -> ()) {
// https://stackoverflow.com/questions/29824488/when-to-use-closures-in-swift
let context = LAContext()
var error: NSError?
if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
foundCompletion()
} else {
notFoundCompletion()
}
}
@objc func setPassword() {
let ac = UIAlertController(title: "App Pasword", message: "Please provide a password!", preferredStyle: .alert)
let saveAction = UIAlertAction(title: "Save", style: .default) {
[weak ac] _ in
guard let text = ac?.textFields?[0].text else { return }
let saveSuccessful: Bool = KeychainWrapper.standard.set(text, forKey: self.KEY_PASSWORD)
print("SaveSuccessful=\(saveSuccessful)")
}
saveAction.isEnabled = false
ac.addAction(saveAction)
ac.addTextField(configurationHandler: { textField in
textField.placeholder = "Password"
textField.isSecureTextEntry = true
NotificationCenter.default.addObserver(forName: UITextField.textDidChangeNotification, object: textField, queue: OperationQueue.main, using: { _ in
let textCount = textField.text?.trimmingCharacters(in: .whitespacesAndNewlines).count ?? 0
let textIsNotEmpty = textCount > 0
guard let repeatPassword = ac.textFields?[1].text else { return }
let same = textField.text == repeatPassword
saveAction.isEnabled = textIsNotEmpty && same
})
})
ac.addTextField(configurationHandler: { textField in
textField.placeholder = "Repeat password"
textField.isSecureTextEntry = true
NotificationCenter.default.addObserver(forName: UITextField.textDidChangeNotification, object: textField, queue: OperationQueue.main, using: { _ in
let textCount = textField.text?.trimmingCharacters(in: .whitespacesAndNewlines).count ?? 0
let textIsNotEmpty = textCount > 0
guard let password = ac.textFields?[0].text else { return }
let same = password == textField.text
saveAction.isEnabled = textIsNotEmpty && same
})
})
ac.addAction(UIAlertAction(title: "Cancel", style: .cancel))
present(ac, animated: true)
}
func shouldShowSettings() {
checkBiometry({
// print("found")
}) {
let text = KeychainWrapper.standard.string(forKey: KEY_PASSWORD) ?? ""
if text == "" {
navigationItem.leftBarButtonItem = setPasswordButton
} else {
navigationItem.leftBarButtonItem = nil
}
}
}
}