iOS 判断设备是否越狱

我们在开发过程中,需要知道设备是否越狱,在网上查看很多资料,为此封装一些判断的方法。

上代码,不解释:

.h文件

#import <Foundation/Foundation.h>

@interface JailBroken : NSObject

/**
* 1. 判定常见的越狱文件
*/
- (BOOL)isJailBrokenOne; /**
* 2. 判断cydia的URL scheme
*/
- (BOOL)isJailBrokenTwo; /**
* 3. 读取系统所有应用的名称
*/
- (BOOL)isJailBrokenThree; /**
* 4. 使用stat方法来判定cydia是否存在
*/
- (BOOL)isJailBrokenFour; /**
* 5. 读取环境变量
*/
- (BOOL)isJailBrokenFive; @end

.m 文件

#import "JailBroken.h"
#import "AppDelegate.h"
#import <sys/stat.h>
#import<dlfcn.h>
#import <dlfcn.h> #define ARRAY_SIZE(a) sizeof(a)/sizeof(a[0])
#define USER_APP_PATH @"/User/Applications/"
#define CYDIA_APP_PATH "/Applications/Cydia.app"
#define APT_APP_PATH "/private/var/lib/apt/" const char* jailbreak_tool_pathes[] = {
"/Applications/Cydia.app",
"/Library/MobileSubstrate/MobileSubstrate.dylib",
"/etc/apt"
}; @implementation JailBroken /**
* 1. 判定常见的越狱文件
*/
- (BOOL)isJailBrokenOne {
for (int i=; i<ARRAY_SIZE(jailbreak_tool_pathes); i++) {
if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithUTF8String:jailbreak_tool_pathes[i]]]) {
NSLog(@"1 The device is jailbroken!");
return YES;
}
}
NSLog(@"1 The device is not jailbroken!");
return NO;
} //- (BOOL)isJailBrokenOne {
// if ([[NSFileManager defaultManager] fileExistsAtPath:@"/etc/apt"]) {
// NSLog(@"1 The device is jailbroken!");
// return YES;
// }
//
// NSLog(@"1 The device is not jailbroken!");
// return NO;
//} /**
* 2. 判断cydia的URL scheme
*/
- (BOOL)isJailBrokenTwo {
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"cydia://"]]) {
NSLog(@"2 The device is jailbroken!");
return YES;
}
NSLog(@"2 The device is not jailbroken!");
return NO;
} /**
* 3. 读取系统所有应用的名称
*/
- (BOOL)isJailBrokenThree {
if ([[NSFileManager defaultManager] fileExistsAtPath:USER_APP_PATH]) {
NSLog(@"3 The device is jailbroken!");
NSArray *list = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:USER_APP_PATH error:nil];
NSLog(@"list = %@",list);
return YES;
}
NSLog(@"3 The device is not jailbroken!");
return NO;
} /**
* 4. 使用stat方法来判定cydia是否存在
*/
- (BOOL)isJailBrokenFour {
if (checkCydia()) {
NSLog(@"4 The device is jailbroken!");
return YES;
}
NSLog(@"4 The device is not jailbroken!");
return NO;
} /**
* 5. 读取环境变量
*/
- (BOOL)isJailBrokenFive {
if (printEnv()) {
NSLog(@"5 The device is jailbroken!");
return YES;
}
NSLog(@"5 The device is not jailbroken!");
return NO;
} #pragma mark - Private int checkInject()
{
int ret;
Dl_info dylib_info;
int (*func_stat)(const char*, struct stat*) = stat; if ((ret = dladdr(func_stat, &dylib_info))) {
return ;
}
return ;
} int checkCydia()
{
// first ,check whether library is inject
struct stat stat_info; if (!checkInject()) {
if ( == stat(CYDIA_APP_PATH, &stat_info) ||( == stat(APT_APP_PATH, &stat_info))) {
return ;
}
} else {
return ;
}
return ;
} char* printEnv(void)
{
char *env = getenv("DYLD_INSERT_LIBRARIES");
NSLog(@"%s", env);
return env;
} @end

在AppDelegate.m 文件

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch. JailBroken *jb = [[JailBroken alloc] init]; if ( [jb isJailBrokenOne] || [jb isJailBrokenTwo] || [jb isJailBrokenThree] || [jb isJailBrokenFour] || [jb isJailBrokenFive] ) {
NSLog(@"越狱!!!");
}
else {
NSLog(@"未越狱!!!");
} return YES;
}
上一篇:DES加密 java与.net可以相互加密解密的方法


下一篇:初识C#扩展方法