您的位置:首页 > 移动开发 > IOS开发

iOS-私有API与runtime

2016-06-30 11:37 459 查看

有朋友在做类似iTool的功能,跟我聊起来,这几天闲,就写了一个demo,不是正经做这个,还很粗略,具体干货诸位等这位朋友自己发文吧。

DEMO

https://github.com/liulishuo/testAppList

思路

iOS9白名单的上限是50个,如果想绕过这个限制,扫描系统中所有app的状态,只有使用私有API,需要用到的类有两个:LSApplicationWorkspace、LSApplicationProxy,知道类的名字我们就可以依靠runtime得到这个类,以及这个类的所有方法,OC的方法望文生义,接下来就可以慢慢尝试。

实现

得到LSApplicationWorkspace、LSApplicationProxy
Class LSApplicationWorkspace_class = objc_getClass("LSApplicationWorkspace");
Class LSApplicationProxy_class = object_getClass(@"LSApplicationProxy");


得到类的所有方法与成员变量(编程小翁)

//获取不到成员变量
int count = 0;
Ivar *members = class_copyIvarList([LSApplicationProxy_class class], &count);
for (int i = 0 ; i < count; i++) {
Ivar var = members[i];
const char *memberName = ivar_getName(var);
const char *memberType = ivar_getTypeEncoding(var);
NSLog(@"%s: %s",memberType,memberName);
}

NSLog(@"count: %d",count);
//获取不到有用的方法
count = 0;
Method *memberMethods = class_copyMethodList(LSApplicationProxy_class, &count);
for (int i = 0; i < count; i++) {
SEL name = method_getName(memberMethods[i]);
NSString *methodName = [NSString stringWithCString:sel_getName(name) encoding:NSUTF8StringEncoding];
NSLog(@"member method:%@", methodName);
}

NSLog(@"count: %d",count);

因为函数class_copyIvarList、class_copyMethodList有时不能返回有用的结果,所以我们使用class-dump(有朋友反映xcode7的库导不出来,大家用源码自己build一个吧),导出类的头文件。

导出MobileCoreServices.framework的所有头文件:

class-dump -H /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/Frameworks/MobileCoreServices.framework  -o /Users/credit/Desktop/header004

-o后面是输出路径 改成你需要的。



MobileCoreServices.framework的所有头文件



LSApplicationProxy



LSApplicationWorkspace

得到app列表
虽然没有注释,但是我们可以猜到这个方法应该可以得到app列表

- (id)allApplications;

但是他是实例方法,我们先要拿到一个LSApplicationWorkspace实例

+ (id)defaultWorkspace;

代码如下

NSObject* workspace = [LSApplicationWorkspace_class performSelector:@selector(defaultWorkspace)];
NSArray *appList = [workspace performSelector:@selector(allApplications)];

遍历app列表

applist里的每一个元素都是LSApplicationProxy 比对其头文件,把对应的属性打印出来研究,

属性略多,不想自己写的朋友,请看我的demo



遍历数组



YY



微信

注意groupContainers数组的内容,我们可以拿到group id,可不可以拿到公共存储区的数据呢?

NSUserDefaults *share = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.tencent.xin"];
NSLog(@"%@",share.dictionaryRepresentation);

然而并没有,那有没有共享数据的目录呢?

NSURL *containerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.com.tencent.xin"];

返回值为nil

app不在group.com.tencent.xin这个组内,咱的冒牌货强写是无效的,因为这个标识符已经被正品占用了。



App Groups

还有什么好玩的? 试试这个

[workspace performSelector:@selector(uninstallApplication:withOptions:) withObject:@"XXX" withObject:nil];

workspace 是LSApplicationWorkspace实例,@"XXX"这里填你获取到的applicationIdentifier

模拟器可以正常卸载app,真机不行。

更多API,大家可以自己尝试一下。

文/cocoa(简书作者)

原文链接:http://www.jianshu.com/p/6167b9ce7af8

著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iOS