Zaktualizowano 21. marca 2026 przez Jan Bunk
Chcesz poprawić bezpieczeństwo swojej aplikacji, nie tracąc na wygodzie? Lokalna autoryzacja może być rozwiązaniem. Ta dokumentacja pokaże Ci, jak poprosić użytkowników aplikacji o podanie PIN-u urządzenia, odcisku palca lub Face ID przed dostępem do części aplikacji.
Włącz uprawnienie biometryczne w ustawieniach uprawnień. W przeciwnym razie ta funkcja może działać tylko częściowo (bez Face ID) lub nieoczekiwanie się psuć.
executeWhenAppReady() w naszym pomocniczym skrypcie aplikacji. Zapewnia, że Twoja strona nie próbuje komunikować się z aplikacją zanim będzie gotowa lub gdy strona jest otwarta w zwykłej przeglądarce (ReferenceError, funkcja nie jest zdefiniowana). requestLocalAuthenticationUżyj tej funkcji, aby poprosić użytkownika o autoryzację.
Funkcja zwraca obiekt z wynikiem typu boolean w przypadku powodzenia. Jeśli okno uwierzytelniania zostanie anulowane albo urządzenie nie może kontynuować, zamiast tego rzuca stringowy kod błędu.
<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>
Starsze wersje aplikacji mogą nadal obsługiwać stringi błędów takie jak NotAvailable, PasscodeNotSet, LockedOut i PermanentlyLockedOut.