判断有没有越狱,以及读系统info.plist

来源:互联网 发布:c语言的书哪本比较好 编辑:程序博客网 时间:2024/05/21 20:47

1。判断是否越狱

detectDevice.h

  1. @interface UIDevice (Helper)  
  2. - (BOOL)isJailbroken;  
  3. @end 

detectDevice.m

  1. @implementation UIDevice (Helper)  
  2. - (BOOL)isJailbroken {  
  3. BOOL jailbroken = NO;  
  4. NSString *cydiaPath = @“/Applications/Cydia.app”;  
  5. NSString *aptPath = @“/private/var/lib/apt/”;  
  6. if ([[NSFileManager defaultManager] fileExistsAtPath:cydiaPath]) {  
  7. jailbroken = YES;  
  8. }  
  9. if ([[NSFileManager defaultManager] fileExistsAtPath:aptPath]) {  
  10. jailbroken = YES;  
  11. }  
  12. return jailbroken;  
  13. }  
  14. @end

然后在你代码中调用[UIDevice currentDevice] isJailbroken], 如果返回YES说明被破解了,为NO,则没被破解

2。越狱的机可以去获取已安装的程序

 NSFileManager* fileManager = [NSFileManager defaultManager];    NSMutableArray* applist = [NSMutableArray arrayWithCapacity:10];    for (NSString *path in [fileManager directoryContentsAtPath:@"/var/mobile/Applications"]) {        for (NSString *subpath in [fileManager directoryContentsAtPath:                                   [NSString stringWithFormat:@"/var/mobile/Applications/%@", path]]) {            if ([subpath hasSuffix:@".app"])            {                NSString* infoplist =  [NSString stringWithFormat:@"/var/mobile/Applications/%@/%@/Info.plist", path, subpath];                NSLog(@"sdfsadfa0%@",infoplist);                NSDictionary* dict =  [NSDictionary dictionaryWithContentsOfFile:infoplist];                NSLog(@"sdfsadfa1%@",dict);                [applist addObject:[dict objectForKey:@"CFBundleDisplayName"]];            }        }

3。如果没有越狱的机 

openURL能帮助你运行Maps,SMS,Browser,Phone甚至其他的应用程序。

openURL的使用方法: 

[[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:myURLString]];

自己定义URL,方法如下: 

打开info.plist,添加一项URL types

展开URL types,再展开Item1,将Item1下的URL identifier修改为URL Scheme
展开URL Scheme,将Item1的内容修改为myapp

或者:

(增加一下此段设置)
<key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>myapp</string>
            </array>
            <key>CFBundleURLName</key>
            <string>com.yourcompany.appName</string>
        </dict>
    </array>

其他程序可通过myapp://访问此自定义URL

可通过[[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"myapp://com.yourcompany.appName"]];

来判断用户机器中是否安装了该程序

最近接触到程序内打开自己,通过第三方控件来调用本身程序:

通过- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url

可以通过处理url来获取信息扫行相应操作。


4,获取ios进程的程序

// .h@interface UIDevice (ProcessesAdditions)- (NSArray *)runningProcesses;@end// .m#import <sys/sysctl.h>@implementation UIDevice (ProcessesAdditions)- (NSArray *)runningProcesses {        int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0};        size_t miblen = 4;                size_t size;        int st = sysctl(mib, miblen, NULL, &size, NULL, 0);                struct kinfo_proc * process = NULL;        struct kinfo_proc * newprocess = NULL;            do {                        size += size / 10;        newprocess = realloc(process, size);                        if (!newprocess){                                    if (process){                free(process);            }                                    return nil;        }                        process = newprocess;        st = sysctl(mib, miblen, process, &size, NULL, 0);                    } while (st == -1 && errno == ENOMEM);                if (st == 0){                                if (size % sizeof(struct kinfo_proc) == 0){                        int nprocess = size / sizeof(struct kinfo_proc);                                        if (nprocess){                                                        NSMutableArray * array = [[NSMutableArray alloc] init];                                        for (int i = nprocess - 1; i >= 0; i--){                                                        NSString * processID = [[NSString alloc] initWithFormat:@"%d", process[i].kp_proc.p_pid];                                        NSString * processName = [[NSString alloc] initWithFormat:@"%s", process[i].kp_proc.p_comm];                                                        NSDictionary * dict = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:processID, processName, nil]                                                                                                                                                 forKeys:[NSArray arrayWithObjects:@"ProcessID", @"ProcessName", nil]];                                        [processID release];                                        [processName release];                                        [array addObject:dict];                                        [dict release];                                }                                        free(process);                                return [array autorelease];                        }                }        }                return nil;}@end// Example usage.NSArray * processes = [[UIDevice currentDevice] runningProcesses];for (NSDictionary * dict in processes){        NSLog(@"%@ - %@", [dict objectForKey:@"ProcessID"], [dict objectForKey:@"ProcessName"]);}