You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

192 lines
4.7 KiB

//
// SecureKeypad.swift
// GME Remit
//
// Created by InKwon Kim on 17/04/2019.
// Copyright © 2019 Gobal Money Express Co. Ltd. All rights reserved.
//
import Foundation
protocol SecureKeypadDelegate: class {
func didComplete(_ encryptedString: String)
}
enum SecureKeypadType {
case ascii
case numberic
}
class SecureKeypad: NSObject {
weak var delegate: SecureKeypadDelegate?
var title = "Enter Your GME Password" {
didSet {
self.updateUI()
}
}
var placeholder = "Enter Your GME Password" {
didSet {
self.updateUI()
}
}
var placeholderFont: UIFont? = UIFont(name: "SanFranciscoDisplay-Regular", size: 15) {
didSet {
self.updateUI()
}
}
private lazy var asciiKeypad = TransKeyView()
private lazy var numberPad = TransKeyView()
private var target: UIViewController!
private var keypadType: SecureKeypadType = .ascii
private var encryptedString = ""
init(target: UIViewController, keypadType: SecureKeypadType = .ascii) {
super.init()
self.target = target
self.keypadType = keypadType
self.initKeypad()
}
deinit {
self.asciiKeypad.mTK_ClearDelegateSubviews()
self.numberPad.mTK_ClearDelegateSubviews()
self.asciiKeypad.delegate = nil;
self.numberPad.delegate = nil;
}
func present(animated: Bool, completion: (() -> Void)? = nil) {
let vc: TransKeyView
switch keypadType {
case .ascii:
vc = self.asciiKeypad
case .numberic:
vc = self.numberPad
}
self.target.present(vc, animated: true, completion: completion)
}
private func updateUI() {
switch self.keypadType {
case .ascii:
self.updateASCIIUI()
case .numberic:
self.updateNumber()
}
}
private func initKeypad(){
switch self.keypadType {
case .ascii:
self.initASCIIKeypad()
self.updateASCIIUI()
case.numberic:
//TODO: Numberic keypad
self.initNumberKeypad()
self.updateNumber()
break
}
}
/// init ASCII keypad
private func initASCIIKeypad() {
asciiKeypad.mTK_Init(self)
asciiKeypad.delegate = self
asciiKeypad.mTK_MakeSecureKey()
asciiKeypad.mTK_ShowMessageIfMaxLength("16자리 입력이 초과되었습니다.")
asciiKeypad.mTK_UseCursor(true)
asciiKeypad.mTK_UseAllDeleteButton(true)
asciiKeypad.mTK_UseNavigationBar(true)
asciiKeypad.mTK_UseVoiceOver(true)
asciiKeypad.mTK_setHideInputPasswordDelay(3)
asciiKeypad.mTK_SetControlCenter(false)
asciiKeypad.mTK_setIgnoreStatusbar(false)
asciiKeypad.mTK_UseShiftOptional(true)
asciiKeypad.mTK_Supported(byDeviceOrientation: SupportedByDevicePortraitAndLandscape)
asciiKeypad.mTK_EnableSamekeyInputDataEncrypt(true)
asciiKeypad.mTK_SetUseBalloonImageButton(true)
asciiKeypad.mTK_LicenseCheck("license_mtranskey")
}
/// update ASCII UI
private func updateASCIIUI() {
asciiKeypad.mTK_SetHint(self.placeholder, font: self.placeholderFont)
asciiKeypad.setKeyboardType(
self,
keypadType: 0,
mTK_inputType: 2,
mTK_inputTitle: self.title,
mTK_cryptType: 0,
mTK_maxLength: 100,
mTK_minLength: 0,
mTK_keypadUpper: false,
mTK_languageType: 1
)
}
private func initNumberKeypad(){
numberPad.mTK_Init(self)
numberPad.delegate = self
numberPad.mTK_MakeSecureKey()
numberPad.mTK_LicenseCheck("license_mtranskey")
numberPad.mTK_ShowMessageIfMinLength("0자리 이상 입력해주세요.")
numberPad.mTK_ShowMessageIfMaxLength("16자리 입력이 초과되었습니다.")
numberPad.mTK_UseVoiceOver(false)
numberPad.mTK_setIgnoreStatusbar(false)
numberPad.mTK_SetControlCenter(false)
numberPad.mTK_UseAllDeleteButton(false)
numberPad.mTK_Supported(byDeviceOrientation: SupportedByDevicePortraitAndLandscape)
}
private func updateNumber(){
numberPad.mTK_SetHint(self.placeholder, font: self.placeholderFont)
numberPad.setKeyboardType(
self,
keypadType: TransKeyKeypadTypeText,
mTK_inputType: 2,
mTK_inputTitle: self.title,
mTK_cryptType: 0,
mTK_maxLength: 16,
mTK_minLength: 0,
mTK_keypadUpper: false,
mTK_languageType: Int(mTK_Language_English.rawValue)
)
}
}
extension SecureKeypad: TransKeyViewDelegate {
func secureInputFinish(_ type: Int) {
let vc: TransKeyView
switch keypadType {
case .ascii:
vc = self.asciiKeypad
case .numberic:
vc = self.numberPad
}
self.encryptedString = vc.mTK_GetSecureData()
if vc.mTK_GetDataLength() == 0 {
self.encryptedString = ""
}
vc.dismiss(animated: true){
self.delegate?.didComplete(self.encryptedString)
}
}
}