moko-biometry
0.4.0indexedEnables biometric user authentication using FaceID and TouchID, integrating seamlessly with Compose Multiplatform and providing robust support for Android and iOS platforms.
Enables biometric user authentication using FaceID and TouchID, integrating seamlessly with Compose Multiplatform and providing robust support for Android and iOS platforms.
This is a Kotlin Multiplatform library that provides authentication by FaceId and TouchId(Fingerprint)
root build.gradle
allprojects {
repositories {
mavenCentral()
}
}
project build.gradle
dependencies {
commonMainApi("dev.icerock.moko:biometry:0.4.0")
// Compose Multiplatform
commonMainApi("dev.icerock.moko:biometry-compose:0.4.0")
// Jetpack Compose (only for android, if you don't use multiplatform)
implementation("dev.icerock.moko:biometry-compose:0.4.0")
}
common
In commonMain we should create ViewModel like:
class SampleViewModel(
val biometryAuthenticator: BiometryAuthenticator
) : ViewModel() {
fun tryToAuth() = viewModelScope.launch {
{
isSuccess = biometryAuthenticator.checkBiometryAuthentication(
requestTitle = .desc(),
requestReason = .desc(),
failureButtonText = .desc(),
allowDeviceCredentials =
)
(isSuccess) {
}
} (throwable: Throwable) {
}
}
}
After create ViewModel, let's integrate on platform.
Android
class MainActivity : AppCompatActivity() {
private viewModel: SampleViewModel
{
.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
viewModel = getViewModel {
SampleViewModel(
biometryAuthenticator = BiometryAuthenticator(
applicationContext = applicationContext
)
)
}
viewModel.biometryAuthenticator.bind(
lifecycle = .lifecycle,
fragmentManager = supportFragmentManager
)
}
}
Compose:
In compose for android, the MainActivity is extended from ComponentActivity, we need to change it to FragmentActivity
class MainActivity : FragmentActivity() {
override fun onCreate {
.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setContent {
biometryFactory: BiometryAuthenticatorFactory = rememberBiometryAuthenticatorFactory()
viewModel = getViewModel {
SampleViewModel(
biometryAuthenticator = biometryFactory.createBiometryAuthenticator()
)
}
BindBiometryAuthenticatorEffect(viewModel.biometryAuthenticator)
}
}
}
iOS:
class SampleViewController: UIViewController {
private var viewModel: SampleViewModel!
override func viewDidLoad() {
super.viewDidLoad()
.viewModel (
biometryAuthenticator: (),
)
}
() {
.viewModel.tryToAuth()
}
}
Additionally, you need add NSFaceIDUsageDescription key in Info.plist of your project:
<key>NSFaceIDUsageDescription</key>
<string>$(PRODUCT_NAME) Authentication with TouchId or FaceID</string>
Compose Multiplatform:
@Composable
fun BiometryScreen() {
biometryFactory: BiometryAuthenticatorFactory = rememberBiometryAuthenticatorFactory()
BiometryScreen(
viewModel = getViewModel(
key = ,
factory = viewModelFactory {
BiometryViewModel(
biometryAuthenticator = biometryAuthenticatorFactory.createBiometryAuthenticator()
)
}
)
)
}
= NavigationScreen(title = ) { paddingValues ->
BindBiometryAuthenticatorEffect(viewModel.biometryAuthenticator)
text: String viewModel.result.collectAsState()
Column(
modifier = Modifier.fillMaxSize().padding(paddingValues),
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(text = text)
Button(onClick = viewModel::onButtonClick) {
Text(text = )
}
}
}
Please see more examples in the sample directory.
biometry library;All development (both new features and bug fixes) is performed in the develop branch. This way master always
contains the sources of the most recently released version. Please send PRs with bug fixes to the develop branch.
Documentation fixes in the markdown files are an exception to this rule. They are updated directly in master.
The develop branch is pushed to master on release.
For more details on contributing please see the contributing guide.
Copyright 2021 IceRock MAG Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance the License.
You may obtain a copy of the License at
http:
Unless applicable law agreed to writing, software
distributed under the License distributed an BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express implied.
See the License the specific language governing permissions
limitations under the License.
Surfaced from shared tags and platforms — no rankings paid for.