Cómo: Solicitar la contraseña del dispositivo

Actualizado el 21. marzo 2026 por Jan Bunk


Si buscas una solución finalizada que solicite una contraseña personalizada (no la contraseña del dispositivo) cuando un usuario abra la aplicación comprueba la función de protección por contraseña.

¿Quieres mejorar la seguridad de tu aplicación manteniendo una gran usabilidad? La autenticación local puede ser el camino a seguir. Esta documentación te mostrará cómo puedes pedir a los usuarios de tu aplicación que introduzcan el PIN, la huella dactilar o el Face ID de su dispositivo antes de acceder a partes de tu aplicación.

Activar la función

Activa el permiso de biometría en tu configuración de permisos. De lo contrario, esta función podría funcionar solo parcialmente (sin Face ID) o romperse de forma inesperada.

Uso de la función JavaScript

Quizá quieras consultar la función executeWhenAppReady() de nuestro script de ayuda de la aplicación. Garantiza que tu sitio web no intente interactuar con la aplicación antes de que esté lista o cuando tu sitio web se cargue utilizando un navegador normal (ReferenceError, function is not defined).

requestLocalAuthentication

Utiliza esta función para pedir al usuario su autenticación.

The function returns an object with a boolean result on success. If the authentication prompt is canceled or the device cannot continue, it throws a string error code instead.


<script>
    try {
        // This message will be displayed to the user along with the prompt.
        let reason = "Please authenticate to access this part of the app";

        // Returns a boolean. True if the authentication was successful, false if the authentication challenge failed.
        // If the prompt is canceled or the device cannot continue, an error string is thrown instead.
        let authenticationResult = (await requestLocalAuthentication(reason))["result"];
    }
    catch (e) {
        switch(e) {
            case "userCanceled":
                // The user canceled the prompt and didn't authenticate.
                break;
            case "authInProgress":
                // Another authentication request is already running.
                break;
            case "noCredentialsSet":
                // No biometrics, PIN, password, or pattern is configured.
                break;
            case "systemCanceled":
                // The system interrupted the authentication attempt, for example when the app gets closed.
                break;

            // All the errors below should be very rare in practice:

            case "timeout":
                // The authentication prompt timed out.
                break;
            case "uiUnavailable":
                // The system cannot show the authentication prompt right now.
                break;
            case "deviceError":
                // A device-level error prevented authentication.
                break;
            case "unknownError":
                // An unknown error occurred.
                break;
            default:
                // Any other error
                break;
        }
    }
</script>
    
Migration for apps last updated before 21st of March 2026

Older app versions may still be handling error strings such as NotAvailable, PasscodeNotSet, LockedOut, and PermanentlyLockedOut.

  • NotAvailable is no longer one specific case. It will usually become noCredentialsSet, uiUnavailable, or unknownError.
  • PasscodeNotSet is now noCredentialsSet.
  • LockedOut and PermanentlyLockedOut don't seem to happen anymore.
  • UserCancelled is now userCanceled.
  • auth_in_progress is now authInProgress.
  • no_activity and no_fragment_activity are now uiUnavailable.