Created
June 13, 2019 09:36
-
-
Save CaiJingLong/95b6c5da8ea9ca0ac08e4aa0bfee9f31 to your computer and use it in GitHub Desktop.
flutter 指纹识别/脸识别
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<uses-permission android:name="android.permission.USE_FINGERPRINT"/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
android.enableJetifier=true | |
android.useAndroidX=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<key>NSFaceIDUsageDescription</key> | |
<string>我要校验你的FaceId,以登录应用</string> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:local_auth/auth_strings.dart'; | |
import 'package:local_auth/local_auth.dart'; | |
var localAuth = LocalAuthentication(); | |
void authLocalAuth() async { | |
//校验本地生物模块 | |
bool canCheckBiometrics = await localAuth.canCheckBiometrics; | |
if (!canCheckBiometrics) { | |
showToast("不支持各种生物模块"); | |
return; | |
} | |
var types = await localAuth.getAvailableBiometrics(); | |
print(types); | |
if (types.isEmpty) { | |
showToast('没有支持的生物模块'); | |
return; | |
} | |
try { | |
bool result; | |
final androidAuthMessages = AndroidAuthMessages( | |
goToSettingsButton: "去设置开启", | |
goToSettingsDescription: "因为你没有给予权限,所以要使用此功能需要去设置开启", | |
cancelButton: "取消", | |
signInTitle: "校验身份", | |
fingerprintSuccess: "校验成功", | |
fingerprintNotRecognized: "无法识别指纹。再试一次。", | |
fingerprintHint: "请正确放置手指", | |
fingerprintRequiredTitle: "您还没设置指纹,无法使用", | |
); | |
final iosAuthMessages = IOSAuthMessages( | |
cancelButton: "取消", | |
lockOut: "失败", | |
goToSettingsButton: "去设置开启", | |
goToSettingsDescription: "因为你没有给予权限,所以要使用此功能需要去设置开启", | |
); | |
if (types.contains(BiometricType.fingerprint)) { | |
// 识别指纹 | |
result = await localAuth.authenticateWithBiometrics( | |
localizedReason: "使用您的指纹来登录App", | |
androidAuthStrings: androidAuthMessages, | |
iOSAuthStrings: iosAuthMessages, | |
); | |
} else if (types.contains(BiometricType.face)) { | |
result = await localAuth.authenticateWithBiometrics( | |
localizedReason: "使用您的面部来登录App", | |
androidAuthStrings: androidAuthMessages, | |
iOSAuthStrings: iosAuthMessages, | |
); | |
} else if (types.contains(BiometricType.iris)) { | |
result = await localAuth.authenticateWithBiometrics( | |
localizedReason: "使用您的虹膜来登录App", | |
androidAuthStrings: androidAuthMessages, | |
iOSAuthStrings: iosAuthMessages, | |
); | |
} | |
showToast( | |
"识别结果 ${result == true ? "成功" : "失败"}", | |
duration: const Duration(seconds: 3), | |
); | |
} on PlatformException catch (e) { | |
/// can use [package:local_auth/auth_strings.dart] | |
print(e.code); | |
print(e.details); | |
print(e.message); | |
showToast("识别出错 ${e.code}"); | |
} catch (e) { | |
showToast("识别出错 ${e.code}"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.awesome_example; | |
import android.os.Bundle; | |
import io.flutter.app.FlutterFragmentActivity; | |
import io.flutter.plugins.GeneratedPluginRegistrant; | |
public class MainActivity extends FlutterFragmentActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
GeneratedPluginRegistrant.registerWith(this); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dependencies: | |
local_auth: ^0.5.2+2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment