-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathViewController.swift
89 lines (70 loc) · 3.52 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
//
// ViewController.swift
// SecretSwift
//
// Created by Julian Moorhouse on 29/08/2019.
// Copyright © 2019 Mindwarp Consultancy Ltd. All rights reserved.
//
import UIKit
import LocalAuthentication
class ViewController: UIViewController {
@IBOutlet var secret: UITextView!
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)
}
@IBAction func authenticateTapped(_ sender: Any) {
let context = LAContext()
var error: NSError?
if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
let reason = "Identifiy yourself!" // for touch id
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) { [weak self] success, authenticationError 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)
}
}
}
} else {
let ac = UIAlertController(title: "Biometry unavailable", message: "Your device is not configured for biometric authentication.", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
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
title = "Secret stuff!"
if let text = KeychainWrapper.standard.string(forKey: "SecretMessage") {
secret.text = text
}
}
@objc func saveSecretMessage() {
guard secret.isHidden == false else { return }
KeychainWrapper.standard.set(secret.text, forKey: "SecretMessage")
secret.resignFirstResponder()
secret.isHidden = true
title = "Nothing to see here"
}
}