iOS-私有API与runtime

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

DEMO

https://github.com/liulishuo/testAppList

思路

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

实现

  • 得到LSApplicationWorkspace、LSApplicationProxy

1
2
Class LSApplicationWorkspace_class = objc_getClass( "LSApplicationWorkspace" );
Class LSApplicationProxy_class = object_getClass(@ "LSApplicationProxy" );

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//获取不到成员变量
  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的所有头文件:

1
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后面是输出路径 改成你需要的。

02.png

MobileCoreServices.framework的所有头文件

03.png

LSApplicationProxy

04.png

LSApplicationWorkspace

  • 得到app列表

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

1
- (id)allApplications;

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

1
+ (id)defaultWorkspace;

代码如下

1
2
NSObject* workspace = [LSApplicationWorkspace_class performSelector:@selector(defaultWorkspace)];
NSArray *appList = [workspace performSelector:@selector(allApplications)];
  • 遍历app列表

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

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

05.png

遍历数组

06.png

YY

07.png

微信

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

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

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

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

返回值为nil

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

001.png

App Groups

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

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

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

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值