iOS 11 disable password autofill accessory view option?

后端 未结 20 2479
孤独总比滥情好
孤独总比滥情好 2020-11-28 04:20

As of now I would like to opt out of the new option iOS 11 gives, that is to suggest passwords in the app. When I run the app on iOS 11 I get the autofill option on top of t

20条回答
  •  粉色の甜心
    2020-11-28 04:30

    From what I can tell, Bem's answer does not work in iOS 12 and Gal Shahar's answer does not account for some edge cases (for example, if a user deletes multiple characters at once). I worked around this using an IBAction, thus removing the necessity to check for iOS version altogether. I'm just a beginner, so this might not be the "best" answer or the most efficient, but it made the most sense to me:

    First, uncheck "Secure Text Entry" in the Storyboard or set it to "false"/"NO" via code for your password UITextField. This will prevent iOS from attempting to AutoFill.

    Then, link your password UITextField to an IBAction. Mine is called on:

    • Editing did begin
    • Editing did change
    • Editing did end

    The IBAction function I wrote determines the differences between a user's starting password and what has been inputed into the password UITextField and creates a new password based on this information:

    class Login: UIViewController {
        var password = ""
    
        override func viewDidLoad() { super.viewDidLoad() }
    
        @IBAction func editPasswordField(_ sender: UITextField) {
            var input = Array(sender.text ?? "")
            var oldPassword = Array(password)
            var newPassword = Array("")
    
            //if character(s) are simply deleted from "passwordField" (not replaced or added to), "cursorPosition" is used to determine which corresponding character(s) need to also be removed from "oldPassword"
            //this is indicated by "input" comprising of only "•" (bullets) and being shorter in length than "oldPassword"
            var onlyBullets = true
            for char in input { if char != "•" { onlyBullets = false } }
            if onlyBullets && input.count < oldPassword.count {
                if let selectedRange = sender.selectedTextRange {
                    let cursorPosition = sender.offset(from: sender.beginningOfDocument, to: selectedRange.start)
                    let prefix = String(oldPassword.prefix(cursorPosition))
                    let suffix = String(oldPassword.suffix(input.count - cursorPosition))
                    input = Array(prefix + suffix)
                } else { input = Array("") }
            }
    
            //if no changes were made via input, input would comprise solely of a number of bullets equal to the length of "oldPassword"
            //therefore, the number of changes made to "oldPassword" via "input" can be measured with "bulletDifference" by calculating the number of characters in "input" that are NOT bullets
            var bulletDifference = oldPassword.count
            for char in input { if char == "•" { bulletDifference -= 1 } }
    
            //the only way "bulletDifference" can be less than 0 is if a user copy-pasted a bullet into "input", which cannot be allowed because it breaks this function
            //if a user pastes bullet(s) into "input", "input" is deleted
            //an edge case not accounted for is pasting a mix of characters and bullets (i.e. "ex•mple") when "oldPassword.count" exceeds the number of bullets in the mixed input, but this does not cause crashes and therefore is not worth preventing
            if bulletDifference < 0 {
                bulletDifference = oldPassword.count
                input = Array("")
            }
    
            //"bulletDifference" is used to remove every character from "oldPassword" that corresponds with a character in "input" that has been changed
            //a changed character in "input" is indicated by the fact that it is not a bullet
            //once "bulletDifference" equals the number of bullets deleted, this loop ends
            var bulletsDeleted = 0
            for i in 0..

    Constructive criticism is appreciated!

提交回复
热议问题