Flutter设备信任管理插件trustdevice_pro_plugin的使用
Flutter设备信任管理插件trustdevice_pro_plugin的使用
一、集成要求
在将TrustDecision提供的SDK产品集成到您公司的APP中时,请注意以下合规性说明:
-
用户信息保护:
- 在用户首次启动App并开始收集信息之前,您的公司应通过交互界面或设计(如隐私政策弹窗)充分告知用户收集、使用和共享个人信息的目的、方法和范围,并获得用户的明确同意。
-
SDK信息收集:
- TrustDecision SDK会收集、处理和使用用户的设备识别信息(IMEI/IDFA)、AndroidID、IMSI、MEID、MAC地址、SIM卡序列号、设备类型、设备型号、系统类型、地理位置、登录IP地址、应用列表、运行进程、传感器信息(光传感器、重力传感器、磁场传感器、加速度传感器、陀螺仪传感器)等设备信息。为了确保合规性,隐私政策应涵盖TrustDecision SDK提供服务并收集、处理和使用相关信息的授权。
二、环境支持
平台 | 支持的系统版本 | 支持的架构 |
---|---|---|
Android | Android 5.0及以上 | armeabi, armeabi-v7a, arm64-v8a, x86 |
iOS | iOS 9.0及以上 | armv7, arm64, x86_64 |
三、集成步骤
1. 安装插件
在项目的pubspec.yaml
文件中添加trustdevice_pro_plugin
依赖:
dependencies:
flutter:
sdk: flutter
...
trustdevice_pro_plugin: ^1.3.6
2. 配置AndroidManifest.xml
在AndroidManifest.xml
文件中声明以下权限:
<manifest>
<!-- 必须 -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<!-- 中国大陆以外 -->
<uses-permission android:name="com.google.android.gms.permission.AD_ID"/>
<!-- 可选,如果不声明,部分设备信息将被放弃 -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<!-- Android 11及以上获取已安装的应用包名 -->
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"
tools:ignore="QueryAllPackagesPermission" />
</manifest>
3. 初始化
确保在用户同意隐私协议后进行初始化。初始化方法如下:
Future<void> initWithOptions(Map<String, dynamic> config)
示例代码:
import 'package:trustdevice_pro_plugin/trustdevice_pro_plugin.dart';
class _MyAppState extends State<MyApp> {
final _trustdeviceProPlugin = TrustdeviceProPlugin();
@override
void initState() {
super.initState();
_initWithOptions();
}
Future<void> _initWithOptions() async {
var options = {
"partner": "your_partner_code", // 替换为你的合作伙伴代码
"appKey": "your_app_key", // 替换为你的应用标识
"appName": "your_app_name", // 替换为你的应用名称
"country": "your_country_code", // 替换为你的数据中心代码
"debug": kDebugMode, // 开发阶段启用调试模式,上线时删除
};
await _trustdeviceProPlugin.initWithOptions(options);
}
}
4. 获取blackBox
在初始化成功后,可以调用getBlackBox
或getBlackBoxAsync
获取blackBox。不要在应用中缓存blackBox,应依赖此函数获取。
// 同步调用
Future<String> getBlackBox()
// 异步调用
Future<String> getBlackBoxAsync()
示例代码:
Future<void> _getBlackBox() async {
var blackBox = await _trustdeviceProPlugin.getBlackBox();
if (blackBox != null) {
print("getBlackBox blackBox: $blackBox");
}
}
Future<void> _getBlackBoxAsync() async {
var blackBox = await _trustdeviceProPlugin.getBlackBoxAsync();
if (blackBox != null) {
print("getBlackBox blackBox: $blackBox");
}
}
5. 最佳实践
- 在
onCreate
方法中初始化并异步获取blackBox:
import 'package:trustdevice_pro_plugin/trustdevice_pro_plugin.dart';
class _MyAppState extends State<MyApp> {
final _trustdeviceProPlugin = TrustdeviceProPlugin();
@override
void initState() {
super.initState();
_initWithOptions();
}
Future<void> _initWithOptions() async {
var options = {
"partner": "your_partner_code",
"appKey": "your_app_key",
"appName": "your_app_name",
"country": "your_country_code",
"debug": kDebugMode,
};
await _trustdeviceProPlugin.initWithOptions(options);
await _trustdeviceProPlugin.getBlackBoxAsync();
}
}
- 在实际业务场景中获取blackBox:
Future<void> _register() async {
var blackBox = await _trustdeviceProPlugin.getBlackBox();
// 处理blackBox数据
}
6. 状态检查
getBlackBox()
成功初始化时返回一个26位字符串,例如:rGPGX1678775227I9NCwcuVJCb
。- 初始化失败时返回一个约5000位的字符串。
7. 获取SDK版本
Future<String> getSDKVersion()
示例代码:
Future<String> _getSDKVersion() async {
var sdkVersion = await _trustdeviceProPlugin.getSDKVersion();
return sdkVersion;
}
四、完整示例Demo
以下是一个完整的Flutter项目示例,展示了如何使用trustdevice_pro_plugin
插件:
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:trustdevice_pro_plugin/trustdevice_pro_plugin.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'trustdevice_pro_plugin',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'trustdevice_pro_plugin'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyAppState();
}
class _MyAppState extends State<MyHomePage> {
final _trustdeviceProPlugin = TrustdeviceProPlugin();
var _mResultString = "";
@override
void initState() {
super.initState();
_initWithOptions();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: ListView(
children: [
ElevatedButton(
onPressed: () async {
var sdkVersion = await _getSDKVersion();
Fluttertoast.showToast(msg: "The sdk version is $sdkVersion", textColor: Colors.white);
},
child: Text("Get SDK Version"),
),
ElevatedButton(
onPressed: () {
_initWithOptions();
},
child: Text("Initialization"),
),
ElevatedButton(
onPressed: () async {
var blackBox = await _getBlackBox();
setState(() {
if (blackBox != null) {
_mResultString = blackBox;
print("getBlackBox blackBox: $_mResultString");
}
});
},
child: Text("Get BlackBox"),
),
ElevatedButton(
onPressed: () async {
var blackBox = await _getBlackBoxAsync();
setState(() {
if (blackBox != null) {
_mResultString = blackBox;
print("getBlackBox blackBox: $_mResultString");
}
});
},
child: Text("Get BlackBox Async"),
),
ElevatedButton(
onPressed: () {
_showLiveness(TDLivenessCallback(
onSuccess: (seqId, errorCode, errorMsg, score, bestImageString, livenessId) {
setState(() {
_mResultString = "Liveness验证成功! seqId: $seqId, livenessId: $livenessId, bestImageString: $bestImageString";
print(_mResultString);
});
},
onFailed: (seqId, errorCode, errorMsg, livenessId) {
setState(() {
_mResultString = "Liveness验证失败!, 错误码: $errorCode, 错误内容: $errorMsg";
print(_mResultString);
});
},
));
},
child: Text("Show Liveness"),
),
Container(
margin: EdgeInsets.fromLTRB(18, 20, 18, 0),
child: Text("Result: $_mResultString"),
)
],
),
);
}
/**
* 请求权限
*/
Future<void> _requestPermission() async {
Map<Permission, PermissionStatus> statuses = await [
Permission.location,
Permission.phone,
].request();
}
/**
* 获取SDK版本号
*/
Future<String> _getSDKVersion() async {
var sdkVersion = await _trustdeviceProPlugin.getSDKVersion();
return sdkVersion;
}
/**
* 初始化配置
*/
Future<void> _initWithOptions() async {
var options = {
"partner": "your_partner_code", // 替换为你的合作伙伴代码
"appKey": "your_app_key", // 替换为你的应用标识
"appName": "your_app_name", // 替换为你的应用名称
"country": "your_country_code", // 替换为你的数据中心代码
"debug": kDebugMode, // 开发阶段启用调试模式,上线时删除
};
await _trustdeviceProPlugin.initWithOptions(options);
}
/**
* 获取BlackBox
*/
Future<String> _getBlackBox() async {
var blackBox = await _trustdeviceProPlugin.getBlackBox();
return blackBox;
}
/**
* 异步获取BlackBox
*/
Future<String> _getBlackBoxAsync() async {
var blackBox = await _trustdeviceProPlugin.getBlackBoxAsync();
return blackBox;
}
/**
* 显示Liveness
*/
Future<void> _showLiveness(TDLivenessCallback callback) async {
String license = "your_license"; // 替换为你的License
await _trustdeviceProPlugin.showLiveness(license, callback);
}
}
五、其他配置
- 保持配置:在
proguard-rules.pro
文件中添加以下配置以防止混淆:
-keep class cn.tongdun.**{*;}
-keep class com.trustdecision.**{*;}
更多关于Flutter设备信任管理插件trustdevice_pro_plugin的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter设备信任管理插件trustdevice_pro_plugin的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,下面是一个关于如何使用 trustdevice_pro_plugin
插件的示例代码。这个插件通常用于设备信任管理,可能涉及设备认证、指纹或面部识别等功能。请注意,实际使用时需要根据插件的文档和API进行具体实现,以下是一个基础示例:
环境配置
首先,确保你的 Flutter 项目已经配置好,并且已经在 pubspec.yaml
文件中添加了 trustdevice_pro_plugin
依赖:
dependencies:
flutter:
sdk: flutter
trustdevice_pro_plugin: ^最新版本号
然后运行 flutter pub get
来获取依赖。
插件初始化与使用
在 MainActivity.kt
(对于 Android)或 AppDelegate.swift
(对于 iOS)中,确保已经按照插件文档进行了必要的初始化。以下示例主要关注 Dart 代码部分。
Dart 代码示例
- 导入插件
import 'package:flutter/material.dart';
import 'package:trustdevice_pro_plugin/trustdevice_pro_plugin.dart';
- 初始化插件
通常,在应用的入口点(如 main.dart
)进行插件的初始化。
void main() {
WidgetsFlutterBinding.ensureInitialized();
TrustDeviceProPlugin.instance.init().then((result) {
if (result) {
print("Plugin initialized successfully");
} else {
print("Plugin initialization failed");
}
runApp(MyApp());
}).catchError((error) {
print("Error initializing plugin: $error");
runApp(MyApp());
});
}
- 使用插件功能
以下示例展示了如何检查设备是否已信任,以及请求用户进行设备信任验证(如指纹或面部识别)。
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Trust Device Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Check Device Trust Status:'),
ElevatedButton(
onPressed: () async {
bool isTrusted = await TrustDeviceProPlugin.instance.isDeviceTrusted();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text("Device is trusted: $isTrusted"),
),
);
},
child: Text('Check Trust Status'),
),
SizedBox(height: 20),
Text('Request Device Trust Verification:'),
ElevatedButton(
onPressed: () async {
try {
bool result = await TrustDeviceProPlugin.instance.requestDeviceTrustVerification();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text("Verification result: $result"),
),
);
} catch (error) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text("Error during verification: $error"),
backgroundColor: Colors.red,
),
);
}
},
child: Text('Request Verification'),
),
],
),
),
),
);
}
}
注意事项
- 权限:确保在 Android 和 iOS 上配置了必要的权限,如生物识别权限。
- 错误处理:在实际应用中,要添加更多的错误处理和用户反馈机制。
- 插件版本:上述代码中的
最新版本号
需要替换为实际的插件版本号。 - 文档参考:请参考
trustdevice_pro_plugin
的官方文档,以获得更多详细的功能说明和API使用指南。
这个示例提供了一个基本框架,展示了如何初始化和使用 trustdevice_pro_plugin
插件。根据实际需求,你可能需要调整和扩展这些代码。