flutter如何检测手机是否越狱
在Flutter开发中,如何检测Android或iOS设备是否已越狱/root?有没有可靠的第三方库或原生代码方案?需要注意哪些平台差异和权限问题?求具体实现示例和兼容性建议。
2 回复
Flutter可通过flutter_jailbreak_detection插件检测越狱。调用JailbreakDetection.jailbroken方法,返回布尔值判断设备是否越狱。适用于iOS和Android平台。
更多关于flutter如何检测手机是否越狱的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
在Flutter中检测手机是否越狱可以通过平台通道调用原生代码实现。以下是具体实现方法:
Android端检测方法:
import 'package:flutter/services.dart';
class JailbreakDetection {
static const MethodChannel _channel = MethodChannel('jailbreak_detection');
static Future<bool> get isJailbroken async {
try {
return await _channel.invokeMethod('isJailbroken');
} on PlatformException {
return false;
}
}
}
Android原生代码(Kotlin):
class MainActivity : FlutterActivity() {
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, "jailbreak_detection").setMethodCallHandler { call, result ->
when (call.method) {
"isJailbroken" -> result.success(isJailbroken())
else -> result.notImplemented()
}
}
}
private fun isJailbroken(): Boolean {
val checks = arrayOf(
"/system/app/Superuser.apk",
"/sbin/su",
"/system/bin/su",
"/system/xbin/su",
"/data/local/xbin/su",
"/data/local/bin/su",
"/system/sd/xbin/su",
"/system/bin/failsafe/su",
"/data/local/su"
)
return checks.any { File(it).exists() } ||
checkBuildTags() ||
checkSuCommand()
}
private fun checkBuildTags(): Boolean {
return Build.TAGS?.contains("test-keys") == true
}
private fun checkSuCommand(): Boolean {
return try {
Runtime.getRuntime().exec("su")
true
} catch (e: Exception) {
false
}
}
}
iOS端检测方法:
// 使用相同的Dart接口
iOS原生代码(Swift):
import Flutter
public class SwiftJailbreakDetectionPlugin: NSObject, FlutterPlugin {
public static func register(with registrar: FlutterPluginRegistrar) {
let channel = FlutterMethodChannel(name: "jailbreak_detection", binaryMessenger: registrar.messenger())
let instance = SwiftJailbreakDetectionPlugin()
registrar.addMethodCallDelegate(instance, channel: channel)
}
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
switch call.method {
case "isJailbroken":
result(isJailbroken())
default:
result(FlutterMethodNotImplemented)
}
}
private func isJailbroken() -> Bool {
if TARGET_OS_SIMULATOR != 0 { return false }
let paths = [
"/Applications/Cydia.app",
"/Library/MobileSubstrate/MobileSubstrate.dylib",
"/bin/bash",
"/usr/sbin/sshd",
"/etc/apt"
]
for path in paths {
if FileManager.default.fileExists(atPath: path) {
return true
}
}
// 检查能否写入系统目录
let stringToWrite = "Jailbreak Test"
do {
try stringToWrite.write(toFile: "/private/jailbreak.txt", atomically: true, encoding: .utf8)
try FileManager.default.removeItem(atPath: "/private/jailbreak.txt")
return true
} catch {
return false
}
}
}
使用方式:
bool isJailbroken = await JailbreakDetection.isJailbroken;
if (isJailbroken) {
print("设备已越狱");
} else {
print("设备未越狱");
}
注意事项:
- 越狱检测不是100%可靠,可能被绕过
- 在iOS模拟器上默认返回未越狱
- 建议结合其他安全检测措施
- 需要分别在Android和iOS项目中注册原生代码
这种方法通过检查常见的越狱文件和系统特征来判断设备是否越狱。

