Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable "Save Password" action sheet when exiting UIViewController?

Tags:

My app has a "Create Account" view controller (shown below) that prompts the user to enter a username and password. Whenever I segue to another view controller, I get a pop-up action sheet prompting to save the password in the keychain.

This is a nifty little freebie IF the user successfully creates the new account. But I get this same pop-up if the user hits the cancel (back) button in the navigation bar, if they select the option to use Facebook login instead of creating an account, or any other means for leaving this view controller (see figures below).

How can I get this popup to ONLY show up when the user successfully creates a new account?

EDIT: Per request, here is the code that is related to the segues that result in the appearance of the "Save Password" action sheet.

from CreateAccountViewController.swift:

class CreateAccountViewController : UIViewController
{
  // ... bunch of irrelevant code deleted ...

  // bound to "Connect with Facebook" button (see image below)
  @IBAction func switchToFacebook(_ sender : UIButton)
  {
    performSegue(.SwitchToFacebookLogin, sender: sender)
  }
  // ... bunch of irrelevant code deleted ...
}

extension CreateAccountViewController : GameServerAlertObserver
{
  // callback based on response from GameCenter after 
  //  submitting a "create new user" request
  func handleConnectionResponse(_ response:GameServerResponse )
  {
    switch response
    {
    // ... other response cases removed ...
    case .UserCreated:
      self.removeSpinner()
      performSegue(.CreateAccountToStartup, sender: self)

    default:
      response.displayAlert(over: self, observer: self)
      self.removeSpinner()
    }
  }

  // Functions defined in the GameServerAlertObserver protocol
  //   to handle user response to "User Exists Popup" (figure below)    
  func ok()
  {
    // user chose to enter new  password... clear the existing username field
    usernameTextField.text = ""
  }

  func cancel()
  {
    // segue back to the startup view controller
    performSegue(.CreateAccountToStartup, sender: self)
  }

  func goToLogin()
  {
    // segue to the login view controller
    performSegue(.SwitchToAccountLogin, sender:self)
  }
}

from UIViewController_Segues:

enum SegueIdentifier : String
{
  case LoserBoard             = "loserBoard"
  case CreateAccount          = "createAccount"
  case AccountLogin           = "accountLogin"
  case FacebookLogin          = "facebookLogin"
  case SwitchToFacebookLogin  = "switchToFacebookLogin"
  case SwitchToAccountLogin   = "switchToAccountLogin"
  case CreateAccountToStartup = "createAccountToStartup"
  case AccountLoginToStartup  = "accountLoginToStartup"
  case FacebookLoginToStartup = "facebookLoginToStartup"
  case UnwindToStartup        = "unwindToStartup"
}

extension UIViewController
{ 
  func performSegue(_ target:SegueIdentifier, sender:Any?)
  {
    performSegue(withIdentifier: target.rawValue, sender: sender)
  }
}

from GameServerAlert.swift:

protocol GameServerAlertObserver
{
  func ok()
  func cancel()
  func goToLogin()
}

extension GameServerResponse
{
  func displayAlert(over controller:UIViewController, observer:GameServerAlertObserver? = nil)
  {
    var title   : String
    var message : String
    var actions : [UIAlertAction]

    switch self
    {
    // ... deleted cases/default which don't lead to segue ...

    case .UserAlreadyExists:
      title = "User already exists"
      message = "\nIf this is you, please use the login page to reconnect.\n\nIf this is not you, you will need to select a different username."
      actions = [
        UIAlertAction(title: "Go to Login page",   style: .default, handler: { _ in observer?.goToLogin() } ),
        UIAlertAction(title: "Enter new username", style: .default, handler: { _ in observer?.ok() } ),
        UIAlertAction(title: "Cancel",             style: .cancel,  handler: { _ in observer?.cancel() } )
      ]
    }

    let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
    actions.forEach { (action) in alert.addAction(action) }
    controller.present(alert,animated:true)
  }
}

Examples from the simulator:

Create Account - (user enters username and password for new account here.)

enter image description here

Facebook Login

If user decides to use Facebook to log in rather than creating a user account, they are taken to this view (which I still haven't fleshed out). Note that the "Save Password" action sheet has popped up.

enter image description here

User Exists Popup

If user attempts to create an account with a username that already exists, they will be presented with this popup. If they select Cancel, they are taken back to the startup screen (see below). If they select Enter new username, they are kept on the same screen with the username cleared out. If they select Login, they are taken to the login screen.

enter image description here

Startup Screen

If the user selects Cancel above, they are brought back here. Again, note that the "Save Password" action sheet has popped up.

enter image description here

like image 721
MikeMayer67 Avatar asked Mar 22 '20 20:03

MikeMayer67


People also ask

How do I stop keychain from asking to save Passwords?

Open the Settings app, then scroll down and tap Passwords. Tap AutoFill Passwords and deselect iCloud Keychain.


1 Answers

What I do to avoid the automatic Password saving action sheet when the user :

  • dismiss the login view controller ;
  • pop the view controller ;
  • use interactive pop gesture.

=>

  override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
      passwordTextField.textContentType = .password
    }
  }

  override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    if isMovingFromParent || isBeingDismissed || parent?.isBeingDismissed == true {
      passwordTextField.textContentType = nil
    }
  }
like image 57
Ded77 Avatar answered Sep 30 '22 19:09

Ded77