Flutter信任验证与安全插件trust_fall的使用
Flutter信任验证与安全插件trust_fall的使用
trust_fall 是一个用于检测设备是否被破解(如越狱或Root)、是否为模拟器或是否可以模拟位置的Flutter插件。它基于以下库实现功能:
- Android Root检测:RootBeer
- iOS 越狱检测:DTTJailbreakDetection
灵感来源于 jail-monkey 和 此博客文章。
开始使用
在你的Flutter项目中添加依赖:
dependencies:
...
trust_fall: ^1.0.4
使用方法
导入包
import 'package:trust_fall/trust_fall.dart';
检测设备是否被越狱或Root
bool isJailBroken = await TrustFall.isJailBroken;
检测设备是否为真实设备
bool isRealDevice = await TrustFall.isRealDevice;
检测设备是否可以模拟位置(无需Root)
bool canMockLocation = await TrustFall.canMockLocation;
检测应用程序是否运行在外部存储上(仅限Android)
bool isOnExternalStorage = await TrustFall.isOnExternalStorage;
检测设备是否违反上述任何规则
bool isTrustFall = await TrustFall.isTrustFall;
注意事项
模拟位置检测
- Android:需要在应用内授予位置权限才能正确检测模拟位置。
- iOS:目前我们通过检测设备是否越狱或是否为真实设备来间接判断。没有强效的iOS模拟位置检测方法(如果你有好的方法,欢迎提交PR)。
提示:由于模拟器通常已被Root,你可能希望在开发期间绕过这些检查,除非你想频繁收到误报。
完整示例代码
以下是一个完整的示例代码,展示如何使用 trust_fall 插件进行设备信任状态检测。
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:trust_fall/trust_fall.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
[@override](/user/override)
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
bool isJailBroken = false;
bool canMockLocation = false;
bool isRealDevice = true;
bool isOnExternalStorage = false;
bool isTrustFall = false;
[@override](/user/override)
void initState() {
super.initState();
initPlatformState();
}
// 初始化平台状态
Future<void> initPlatformState() async {
try {
isJailBroken = await TrustFall.isJailBroken;
canMockLocation = await TrustFall.canMockLocation;
isRealDevice = await TrustFall.isRealDevice;
isOnExternalStorage = await TrustFall.isOnExternalStorage;
isTrustFall = await TrustFall.isTrustFall;
} catch (error) {
print(error);
}
setState(() {});
}
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('设备信任状态检测'),
),
body: Center(
child: Card(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
// 是否越狱/Root
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('isJailBroken():'),
SizedBox(width: 8),
Text('${isJailBroken ? "是" : "否"}', style: TextStyle(fontWeight: FontWeight.w600)),
],
),
SizedBox(height: 8),
// 是否可以模拟位置
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('canMockLocation():'),
SizedBox(width: 8),
Text('${canMockLocation ? "是" : "否"}', style: TextStyle(fontWeight: FontWeight.w600)),
],
),
SizedBox(height: 8),
// 是否为真实设备
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('isRealDevice():'),
SizedBox(width: 8),
Text('${isRealDevice ? "是" : "否"}', style: TextStyle(fontWeight: FontWeight.w600)),
],
),
SizedBox(height: 8),
// 是否运行在外部存储上
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('isOnExternalStorage():'),
SizedBox(width: 8),
Text('${isOnExternalStorage ? "是" : "否"}', style: TextStyle(fontWeight: FontWeight.w600)),
],
),
SizedBox(height: 8),
// 是否违反信任规则
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('isTrustFall():'),
SizedBox(width: 8),
Text('${isTrustFall ? "是" : "否"}', style: TextStyle(fontWeight: FontWeight.w600)),
],
),
],
),
),
),
),
),
);
}
}
更多关于Flutter信任验证与安全插件trust_fall的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter信任验证与安全插件trust_fall的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
在使用Flutter开发移动应用时,确保应用的安全性和用户数据的保护是非常重要的。为了防止应用在非安全的环境中运行,开发者可以使用一些插件来检测设备是否被root或越狱,以及应用是否运行在模拟器上。trust_fall 是一个常用的Flutter插件,用于检测设备的安全性。
1. 安装 trust_fall 插件
首先,你需要在 pubspec.yaml 文件中添加 trust_fall 插件的依赖:
dependencies:
flutter:
sdk: flutter
trust_fall: ^0.1.0
然后,运行 flutter pub get 来安装插件。
2. 使用 trust_fall 插件
trust_fall 插件提供了几个方法来检测设备的安全性。以下是一些常用的方法:
检测设备是否被root或越狱
import 'package:trust_fall/trust_fall.dart';
void checkDeviceSecurity() async {
bool isJailbroken = await TrustFall.isJailbroken;
bool isRealDevice = await TrustFall.isRealDevice;
if (isJailbroken) {
print("Device is jailbroken or rooted!");
} else {
print("Device is not jailbroken or rooted.");
}
if (isRealDevice) {
print("Running on a real device.");
} else {
print("Running on a simulator or emulator.");
}
}
检测应用是否运行在模拟器上
import 'package:trust_fall/trust_fall.dart';
void checkIfSimulator() async {
bool isSimulator = await TrustFall.isSimulator;
if (isSimulator) {
print("App is running on a simulator.");
} else {
print("App is running on a real device.");
}
}
检测应用是否被篡改
import 'package:trust_fall/trust_fall.dart';
void checkIfAppIsTampered() async {
bool isTampered = await TrustFall.isAppTampered;
if (isTampered) {
print("App has been tampered!");
} else {
print("App is not tampered.");
}
}
3. 处理检测结果
根据检测结果,你可以采取不同的措施。例如,如果设备被root或越狱,你可以选择阻止用户继续使用应用,或者显示一个警告消息。
void handleSecurityCheck() async {
bool isJailbroken = await TrustFall.isJailbroken;
bool isSimulator = await TrustFall.isSimulator;
if (isJailbroken || isSimulator) {
// 显示警告或阻止用户继续使用应用
print("This app cannot run on a rooted/jailbroken device or simulator.");
} else {
// 继续正常应用逻辑
print("Device is secure, continuing...");
}
}

