判断手机是否root或越狱
给客户开发的app通过第三方检测机构检测出来一些漏洞,其中一项判断手机是否root或者越狱,如果是的话需要加提示,从网上找了一些方法,修改一下用到了项目中
-
判断Android手机是否root
//在app的入口activity的onCreate方法中添加如下一段代码 if (CommonUtil.checkRoot()) { new AlertDialog.Builder(this) .setTitle("当前设备已ROOT,存在安全风险,请更换其他设备") .setMessage("") .setPositiveButton("退出应用", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { System.exit(0);//退出app } }) .setCancelable(false) .show(); } //判断是否root方法 public static boolean checkRoot() { boolean isRoot = false; String buildTags = Build.TAGS; if (buildTags!=null && buildTags.contains("test-keys")) { isRoot = true; } String[] paths = { "/system/app/Superuser.apk", "/sbin/su", "/system/bin/su", "/system/xbin/su", "/data/local/xbin/su", "/data/local/bin/su", "/data/local/su" }; for (String path : paths) { if (new File(path).exists()) isRoot = true; } return isRoot; }
-
判断iOS手机是否越狱
//在AppDelegate的didFinishLaunchingWithOptions中添加下面代码 BOOL isBroken = [self checkJailbreaking]; if (isBroken) [self showJailbreakingAlert]; //检测是否越狱 - (BOOL)checkJailbreaking { BOOL isBroken = NO; struct stat stat_info; if (0 == stat("/Applications/Cydia.app", &stat_info)) { isBroken = YES; } int ret ; Dl_info dylib_info; int (*func_stat)(const char *, struct stat *) = stat; if ((ret = dladdr(func_stat, &dylib_info))) { if (strcmp(dylib_info.dli_fname,"/usr/lib/system/libsystem_kernel.dylib") != 0) { isBroken = YES; } } char *env = getenv("DYLD_INSERT_LIBRARIES"); if (env) isBroken = YES; return isBroken; } //弹出提示框 - (void)showJailbreakingAlert { UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"当前设备已ROOT,存在安全风险,请更换其他设备" message:@"" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"退出应用" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { exit(0); }]; [alert addAction:cancel]; [self.window.rootViewController presentViewController:alert animated:YES completion:NULL]; }
记录一下,下次再用到可以直接从博客里找
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义