Flutter越狱检测插件flutter_jailbreak_detection_unofficial的使用

Flutter越狱检测插件flutter_jailbreak_detection_unofficial的使用

flutter_jailbreak_detection 是一个用于检测设备是否被越狱或ROOT的Flutter插件。在Android上,它使用了 RootBeer 库,在iOS上则使用了 IOSSecuritySuite 库。

使用步骤

  1. 导入库

    在你的Dart文件中导入 flutter_jailbreak_detection 包。

    import 'package:flutter_jailbreak_detection/flutter_jailbreak_detection.dart';
    
  2. 初始化状态

    在应用启动时,通过调用 initPlatformState() 方法来获取当前设备是否被越狱或处于开发者模式。

    bool jailbroken = await FlutterJailbreakDetection.jailbroken;
    bool developerMode = await FlutterJailbreakDetection.developerMode; // android only.
    

完整示例代码

以下是一个完整的示例代码,展示了如何使用 flutter_jailbreak_detection 插件来检测设备是否被越狱。

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:flutter_jailbreak_detection/flutter_jailbreak_detection.dart';

void main() => runApp(new MyApp());

class MyApp extends StatefulWidget {
  [@override](/user/override)
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {
  bool? _jailbroken;
  bool? _developerMode;

  [@override](/user/override)
  void initState() {
    super.initState();
    initPlatformState();
  }

  // 平台消息是异步的,因此我们需要在一个异步方法中进行初始化。
  Future<void> initPlatformState() async {
    bool jailbroken;
    bool developerMode;
    // 平台消息可能会失败,所以我们使用try/catch来捕获PlatformException。
    try {
      jailbroken = await FlutterJailbreakDetection.jailbroken;
      developerMode = await FlutterJailbreakDetection.developerMode;
    } on PlatformException {
      jailbroken = true;
      developerMode = true;
    }

    // 如果小部件在异步平台消息处理过程中从树中移除,则应丢弃回复而不是调用setState来更新我们的非存在的外观。
    if (!mounted) return;

    setState(() {
      _jailbroken = jailbroken;
      _developerMode = developerMode;
    });
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Jailbroken plugin example app'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text('Jailbroken: ${_jailbroken == null ? "Unknown" : _jailbroken! ? "YES" : "NO"}'),
              Text('Developer mode: ${_developerMode == null ? "Unknown" : _developerMode! ? "YES" : "NO"}')
            ],
          ),
        ),
      ),
    );
  }
}

更多关于Flutter越狱检测插件flutter_jailbreak_detection_unofficial的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter越狱检测插件flutter_jailbreak_detection_unofficial的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


flutter_jailbreak_detection_unofficial 是一个用于检测设备是否越狱(iOS)或是否已获取 root 权限(Android)的 Flutter 插件。这个插件可以帮助开发者检测设备的安全状态,从而采取相应的措施来保护应用的安全性。

安装

首先,你需要在你的 Flutter 项目中添加 flutter_jailbreak_detection_unofficial 插件。打开 pubspec.yaml 文件,并在 dependencies 部分添加以下内容:

dependencies:
  flutter:
    sdk: flutter
  flutter_jailbreak_detection_unofficial: ^1.0.0

然后运行 flutter pub get 来安装插件。

使用

在你的 Dart 代码中,你可以通过以下方式来使用 flutter_jailbreak_detection_unofficial 插件:

  1. 导入插件
import 'package:flutter_jailbreak_detection_unofficial/flutter_jailbreak_detection_unofficial.dart';
  1. 检测设备是否越狱或已 root
void checkDeviceStatus() async {
  bool isJailbroken = await FlutterJailbreakDetection.isJailbroken;
  bool isDeveloperMode = await FlutterJailbreakDetection.developerMode;
  bool canMockLocation = await FlutterJailbreakDetection.canMockLocation;

  print('Is Jailbroken: $isJailbroken');
  print('Is Developer Mode: $isDeveloperMode');
  print('Can Mock Location: $canMockLocation');

  if (isJailbroken || isDeveloperMode || canMockLocation) {
    // 设备可能不安全,采取相应措施
    print('设备可能不安全');
  } else {
    // 设备安全
    print('设备安全');
  }
}
  1. 在应用启动时检测

你可以在 initState 或其他适当的时机调用 checkDeviceStatus 函数来检测设备状态。

class MyApp extends StatefulWidget {
  [@override](/user/override)
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  [@override](/user/override)
  void initState() {
    super.initState();
    checkDeviceStatus();
  }

  void checkDeviceStatus() async {
    bool isJailbroken = await FlutterJailbreakDetection.isJailbroken;
    bool isDeveloperMode = await FlutterJailbreakDetection.developerMode;
    bool canMockLocation = await FlutterJailbreakDetection.canMockLocation;

    print('Is Jailbroken: $isJailbroken');
    print('Is Developer Mode: $isDeveloperMode');
    print('Can Mock Location: $canMockLocation');

    if (isJailbroken || isDeveloperMode || canMockLocation) {
      // 设备可能不安全,采取相应措施
      print('设备可能不安全');
    } else {
      // 设备安全
      print('设备安全');
    }
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('越狱检测示例'),
        ),
        body: Center(
          child: Text('检测设备安全状态'),
        ),
      ),
    );
  }
}
回到顶部