私は「••••••••••••••」とsecureTextEntry = true
とラベルの文字列を変更しました。それは完璧に動作しますが、文字列の最後の4文字を除いてどのように文字列全体を変更することができますか?secureTextEntry - 迅速
答えて
ここケースは、あなたがこの文字列を使用する方法によって異なります:UILabelで、またはのUITextFieldに。いずれにせよ、ここで最高のものはそうのような文字列を拡張することで、次の操作を実行することです:あなたはこれを必要とする方法に応じて
extension String {
init(withSecureShowOnlyLast4 values : String) {
self.init()
let array = [Character](values.characters)
if array.count <= 4 {
self = values
} else {
var first = array.count - 4
var lastFour : [Character] = []
while first <= (array.count - 1) {
lastFour.append(array[first])
first += 1
}
self = String(Array(repeating: "•", count: array.count - 4) + lastFour)
}
}
}
let pass : String = "fesnjufodpsk"
let obfs = String(withSecureShowOnlyLast4: pass) //prints : ••••••••dpsk
、これは私がそれを使用したい方法です:
UILABEL:
は、ここで必要なデータの安全な保管を確保するために、カスタムクラスです。
class SafeLabel : UILabel {
var makeSafe : Bool = false
private var safeKey : String?
private var alternateSet = Bool()
override var text: String? {
didSet {
if makeSafe && !alternateSet {
alternateSet = true
safeKey = text
self.text = nil
} else if alternateSet {
alternateSet = false
}
}
willSet {
if makeSafe && !alternateSet {
self.safeKey = text
}
}
}
var safe : String {
get {
guard let sa = safeKey else {
return ""
}
guard makeSafe else {
return sa
}
return String(withSecureShowOnlyLast4: sa)
}
}
}
let lab = SafeLabel()
lab.makeSafe = true
lab.text = "9j3od3dkuhosfg"
print(lab.safe) //prints ••••••••••osfg
データの印刷と使用をすべて削除することなく削除する.makeSafeの使用に注意してください。 UITextFieldのFOR
:
は、このクラスのために、私は、文字列を変更するtextField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
メソッドを使用します。ここで再び、私はそうのようにTextFieldをサブクラス化し、このような保護された文字列を変更したい:
class SafeField : UITextField {
var makeSafe : Bool = false
private var safeKey : String?
private var alternateSet = Bool()
override var text: String? {
didSet {
if makeSafe && !alternateSet {
alternateSet = true
safeKey = text
self.text = nil
} else if alternateSet {
alternateSet = false
}
}
willSet {
guard text != nil else {
if let t = safeKey, t.characters.count == 1 || t.isEmpty {
self.safeKey = nil
} else {
isDeletingEntry()
}
return
}
if makeSafe && !alternateSet, let t = text {
if let s = safeKey {
self.safeKey = s + t
} else {
self.safeKey = t
}
}
}
}
var safe : String {
get {
guard let sa = safeKey else {
return ""
}
guard makeSafe else {
return sa
}
return String(withSecureShowOnlyLast4: sa)
}
}
override init(frame: CGRect) {
super.init(frame: frame)
}
private func isDeletingEntry() {
if let old = safeKey {
let new = String(old.characters.dropLast())
self.text = new
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class View: UIView, UITextFieldDelegate {
//Of course, this can be a ViewController, or anything that can handle the
//code bellow
var safe : SafeField = {
var s = SafeField()
s.makeSafe = true
return s
}()
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(safe)
safe.frame = .zero
safe.delegate = self
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard safe != textField else {
if string.characters.isEmpty {
safe.text = nil
} else {
safe.text = string
}
return false
}
return true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
ここで明らかに
あなたはtext
ので、値を取得するが、常にnil
にハードコードされたときにsafe
の代わりtext
を使用する必要があり、私は、難読化されたラベルが常にUI開発者の少数派であることを考慮して、問題ではないと仮定します。 TextFieldから値を取得するには、文字列の曖昧さのない値を取得するために、makeSafe変数をfalseに設定する必要があります。主な焦点としてセキュリティでは、ここでの利点は、あなたが、それは別のファイルから直接値ですアクセス何かまたは誰かを心配することなく、あたりの必要性に基づいどこでもmakeSafe変数を設定し、設定を解除するようにコードを設定できるということです。
もちろん、UILabelを直接変更するためにキーボード要素や何らかのプロトコルを使用している場合は、SafeLieldのテキスト変数をSafeFieldのテキスト変数と入れ替えてください。 (カスタムプロトコルを使用している場合は、ラベルを確認してくださいオプションの鋳造によって実際SafeLabelです:
func protocolMethod(label: UILabel, doSomething: Bool) {
guard let lab = label as? SafeLabel else {
//do your stuff
return
}
//Here you can manipulate lab as a SafeLabel and modify it according to the SafeLabel SubClass
}
あなたはlabel.isSecureTextEntry = true
を落とし、代わりに使用することができます。
var string = label.text!
var result = String()
if string.characters.count > 4{
for i in 1...string.characters.count - 4{
result += "•"
}
string = string[string.index(string.startIndex, offsetBy: string.characters.count-4)..<string.endIndex]
string = result + string
}
label.text = string
- 1. 迅速
- 2. 迅速
- 3. 迅速
- 4. - 迅速
- 5. 迅速
- 6. 迅速
- 7. 迅速
- 8. 迅速
- 9. は迅速
- 10. 迅速なラインコントロールステートメント
- 11. 迅速MySQLの
- 12. 述語 - 迅速
- 13. 迅速なエンドツーエンドテスト
- 14. 迅速UIAlertControllerは
- 15. IOSに迅速
- 16. ページインジケータ迅速
- 17. 迅速絵
- 18. AVFoundation miniFrameDuration迅速
- 19. が迅速
- 20. エラーが迅速
- 21. インスタンスメソッド迅速
- 22. 迅速なエラーシミュレータ
- 23. Firebaseクエリ迅速
- 24. 迅速な3
- 25. は迅速
- 26. が迅速
- 27. DispatchQueue.main.sync迅速
- 28. 迅速 - IOS - UISwitch
- 29. 迅速ダイナミックUIAlertController
- 30. シングルライン:迅速
だから、あなたは最後の4を除いて、すべての文字が「•」になりたい –
あなたはありますか?文字列をUILabelまたはUITextFieldに適用しますか? – murphguy