由于最近在做App內(nèi)購(gòu)贼涩,需要保證支付安全顾瞻,對(duì)于已越獄的iOS 設(shè)備全部不允許內(nèi)購(gòu)行為宿接,簡(jiǎn)單粗暴有效焦匈,可以通過(guò)下面工具的方法判斷iOS設(shè)備是否越獄血公。
.h 文件
#import <Foundation/Foundation.h>
@interface JailbreakDetectTool : NSObject
/**
* 檢查當(dāng)前設(shè)備是否已經(jīng)越獄。
*/
+ (BOOL)detectCurrentDeviceIsJailbroken;
@end
.m 文件
#import "JailbreakDetectTool.h"
#define ARRAY_SIZE(a) sizeof(a)/sizeof(a[0])
@implementation JailbreakDetectTool
// 四種檢查是否越獄的方法, 只要命中一個(gè), 就說(shuō)明已經(jīng)越獄.
+ (BOOL)detectCurrentDeviceIsJailbroken {
BOOL result = NO;
result = [self detectJailBreakByJailBreakFileExisted];
if (!result) {
result = [self detectJailBreakByAppPathExisted];
}
if (!result) {
result = [self detectJailBreakByEnvironmentExisted];
}
if (!result) {
result = [self detectJailBreakByCydiaPathExisted];
}
return result;
}
/**
* 判定常見的越獄文件
* /Applications/Cydia.app
* /Library/MobileSubstrate/MobileSubstrate.dylib
* /bin/bash
* /usr/sbin/sshd
* /etc/apt
* 這個(gè)表可以盡可能的列出來(lái)缓熟,然后判定是否存在累魔,只要有存在的就可以認(rèn)為機(jī)器是越獄了。
*/
const char* jailbreak_tool_pathes[] = {
"/Applications/Cydia.app",
"/Library/MobileSubstrate/MobileSubstrate.dylib",
"/bin/bash",
"/usr/sbin/sshd",
"/etc/apt"
};
+ (BOOL)detectJailBreakByJailBreakFileExisted {
for (int i = 0; i<ARRAY_SIZE(jailbreak_tool_pathes); i++) {
if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithUTF8String:jailbreak_tool_pathes[i]]]) {
NSLog(@"The device is jail broken!");
return YES;
}
}
NSLog(@"The device is NOT jail broken!");
return NO;
}
/**
* 判斷cydia的URL scheme.
*/
+ (BOOL)detectJailBreakByCydiaPathExisted {
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"cydia://"]]) {
NSLog(@"The device is jail broken!");
return YES;
}
NSLog(@"The device is NOT jail broken!");
return NO;
}
/**
* 讀取系統(tǒng)所有應(yīng)用的名稱.
* 這個(gè)是利用不越獄的機(jī)器沒有這個(gè)權(quán)限來(lái)判定的够滑。
*/
#define USER_APP_PATH @"/User/Applications/"
+ (BOOL)detectJailBreakByAppPathExisted {
if ([[NSFileManager defaultManager] fileExistsAtPath:USER_APP_PATH]) {
NSLog(@"The device is jail broken!");
NSArray *applist = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:USER_APP_PATH error:nil];
NSLog(@"applist = %@", applist);
return YES;
}
NSLog(@"The device is NOT jail broken!");
return NO;
}
/**
* 這個(gè)DYLD_INSERT_LIBRARIES環(huán)境變量垦写,在非越獄的機(jī)器上應(yīng)該是空,越獄的機(jī)器上基本都會(huì)有Library/MobileSubstrate/MobileSubstrate.dylib.
*/
char* printEnv(void) {
char *env = getenv("DYLD_INSERT_LIBRARIES");
return env;
}
+ (BOOL)detectJailBreakByEnvironmentExisted {
if (printEnv()) {
NSLog(@"The device is jail broken!");
return YES;
}
NSLog(@"The device is NOT jail broken!");
return NO;
}
@end