Flutter如何检测越狱设备

在Flutter开发中,如何检测设备是否已越狱?目前项目需要对越狱设备做特殊处理,但找不到可靠的Flutter插件或方法来实现这个功能。有人能分享具体的实现方案或推荐可用的第三方库吗?最好能支持iOS和Android双平台检测。

2 回复

Flutter可通过flutter_jailbreak_detection插件检测越狱设备。该插件提供jailbroken方法返回布尔值,判断设备是否越狱。适用于iOS和Android平台,简单易用。

更多关于Flutter如何检测越狱设备的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


在Flutter中检测越狱设备通常需要结合平台特定的代码(通过MethodChannel调用原生API)。以下是常见的方法:

1. iOS端检测方法:

import 'package:flutter/services.dart';

class JailbreakCheck {
  static const platform = MethodChannel('jailbreak_check');

  static Future<bool> get isJailbroken async {
    try {
      return await platform.invokeMethod('isJailbroken');
    } on PlatformException {
      return false;
    }
  }
}

iOS原生代码(Swift):

import Flutter

func isJailbroken() -> Bool {
    // 常见越狱检测路径
    let paths = [
        "/Applications/Cydia.app",
        "/Library/MobileSubstrate/MobileSubstrate.dylib",
        "/bin/bash",
        "/usr/sbin/sshd"
    ]
    
    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)
        return true
    } catch {
        return false
    }
}

2. Android端检测方法:

static Future<bool> get isRooted async {
  try {
    return await platform.invokeMethod('isRooted');
  } on PlatformException {
    return false;
  }
}

Android原生代码(Kotlin):

fun isRooted(): Boolean {
    // 检查常见Root文件
    val paths = arrayOf(
        "/system/app/Superuser.apk",
        "/sbin/su",
        "/system/bin/su",
        "/system/xbin/su"
    )
    
    paths.forEach { path ->
        if (File(path).exists()) return true
    }
    
    // 检查Build.TAGS
    return Build.TAGS?.contains("test-keys") == true
}

注意事项:

  1. 越狱检测不是100%可靠,可能存在误判
  2. 建议在敏感操作前进行检测
  3. 可结合多种检测方法提高准确性

需要将原生代码分别添加到iOS和Android项目中,并在Flutter中通过MethodChannel调用。

回到顶部