Cycript(三):使用技巧

本文详细介绍了Cycript中的关键功能,如获取Objective-C对象、成员变量操作、Bundle ID获取、方法查找、子类列举、框架加载、文件包含、NSLog使用等,是深入理解Cycript的强大工具指南。

获取对象(Getting objects)

  • 使用 choose 函数获取 Objective-C 对象(Objective-C objects using choose)

    在 Cycript 0.9.502 版本中引入并在此处记录的 choose() 函数,允许我们获取某个类所有的现有对象的数组
    (hcg 注:简单地说,就是在内存的堆空间中查找指定的类及其子类的所有实例对象)

  • 使用地址获取 Objective-C 对象(Objective-C objects from addresses)

    cy# var p = #0x8614390
    cy# p
    ["<SKPaymentTransaction: 0x8613d80>"]
    
  • 获取 Javascript 变量(Javascript variables)

    // 需要测试
    cy# typedef int a;
    cy# for (x in this) if (x == 'a') system.print('yay');
    

获取成员变量(Getting ivars)

通常,只需要输入 *varName 即可:

cy# *controller
{
   
   isa:"PrefsRootController",_contentView:"<UIView: 0x10bd70; frame = (0 0; 320 460); autoresize = W+H; layer = <CALayer: 0x150120>>",_navBar:...

有时,*varName 会不起作用:

cy# *UIApp
{
   
   message:"hasProperty callback returned true for a property that doesn't exist.",name:"ReferenceError"}

此时,你可以这样做:

cy# [i for (i in *UIApp)]
["isa","_delegate","_touchMap","_exclusiveTouchWindows","_event",...

你也可以使用下面的 tryPrintIvars 函数获取尽可能多的成员变量以及它们的值:

function tryPrintIvars(a){
   
    var x={
   
   }; for(i in *a){
   
    try{
   
    x[i] = (*a)[i]; } catch(e){
   
   } } return x; }
// 上面的代码经过整理后,如下所示:
function tryPrintIvars(a) {
   
    
	var x={
   
   }; 
	for(i in *a) {
   
    
		try {
   
    x[i] = (*a)[i]; } 
		catch(e) {
   
   } 
	} 
	return x; 
}

tryPrintIvars 函数的使用示例:

cy# *a
{
   
   message:"hasProperty callback returned true for a property that doesn't exist.",name:"ReferenceError"}
cy# tryPrintIvars(a)
{
   
   isa:"SBWaveView",_layer:"<CALayer: 0x2a5160>",_tapInfo:null,_gestureInfo:null,_gestureRecognizers:...

获取 BundleID(Getting bundle identifier)

NSBundle.mainBundle.bundleIdentifier

获取方法(Getting methods)

下面的 printMethods 函数用于获取方法:

// @param.className 类名
// @param.isa		如果为 undefined,则打印对象方法。否则,打印类方法
function printMethods(className, isa) {
   
   
	var count = new new Type("I");
	var classObj = (isa != undefined) ? objc_getClass(className).constructor : objc_getClass(className);
	var methods = class_copyMethodList(classObj, count);
	var methodsArray = [];
	for(var i = 0; i < *count; i++) {
   
   
		var method = methods[i];
		methodsArray.push({
   
   selector:method_getName(method), implementation:method_getImplementation(method)});
	}
	free(methods);
	return methodsArray;
}

printMethods 函数的使用示例:

cy# printMethods("MailboxPrefsTableCell")
[{
   
   selector:@selector(layoutSubviews),implementation:0x302bf2e9},{
   
   selector:@selector(setCurrentMailbox:),implementation:0x302bee0d},...

你也可以只查看 isaprototype 属性。例如,获取 rootViewController 的方法:

UIApp.keyWindow.rootViewController.isa.prototype
  • 获取名称与特定正则表达式匹配的方法(Get methods matching particular RegExp)

    function methodsMatching(cls, regexp) {
         
          return [[new Selector(m).type(cls), m] for (m in cls.prototype) if (!regexp || regexp.test(m))]; }
    
    // 上面的代码经过整理后,如下所示:
    function methodsMatching(cls, regexp) {
         
          
    	return [
    			 [new Selector(m).type(cls), m] 
    			 for (m in cls.prototype) 
    			 	if (!regexp || regexp.test
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值