###innerConsole

innerConsole for App, give you a monitor without Xcode

  1. (base)convience insight your log without Xcode;
  2. (plugin00)convience way check replace viewframe;
  3. (plugin01)dump your userPlist file;
  4. (plugin02)dumo your object methodList or varList;

demo00

demo00

inner

###finish两种‘Obj-c Runtime’级别操作技术

####associated object:汉化过来,应该叫“对象关联”。

  • 原理:
    • associated object指的是一个类的变量列表、方法列表中的绑定关系。通过知道类与变量列表绑定在一起的实现技术(associated object),手动地更改一个类的既定变量列表。
  • 操作:
    • objc_setAssociatedObject
    • objc_getAssociatedObject
    • objc_removeAssociatedObjects
NSObject+AssociatedObject.h

@interface NSObject (AssociatedObject)

@property (nonatomic, strong) id associatedObject;

@end

NSObject+AssociatedObject.m

@implementation NSObject (AssociatedObject)

@dynamic associatedObject;

- (void)setAssociatedObject:(id)object  {

    objc_setAssociatedObject(self, @selector(associatedObject), object, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

    }
     
- (id)associatedObject {

    return objc_getAssociatedObject(self, @selector(associatedObject));

    }
  • 用途:通过associated object在分类中给已存在的类中添加自定义属性。

####method swizzling:汉化过来,应该叫“方法桥接”。

  • 原理
    • Method swizzling指的是改变一个已存在的选择器sel对应的实现imp的过程,它依赖于Objectvie-C实现中方法在调用时能够在“运行时runtime”进行改变——通过改变类的调度表(dispatch table)中选择器sel到最终函数imp间的映射dispatch table关系。
    • Method Swizzling就是改变类的调度表dispatch map让消息解析时从一个选择器sel对应到其他的实现imp,同时将原始的方法实现混淆到一个新的选择器。
  • 操作
    • 获取method的sel:@selector(viewWillAppear:);
    • 通过sel获取method:class_getInstanceMethod(class, originalSelector);
    • 为class(实例)添加method
class_addMethod(class,originalSelector,method_getImplementation(swizzledMethod),method_getTypeEncoding(swizzledMethod));
	
1
* **为class更换method**:
class_replaceMethod(class,swizzledSelector,method_getImplementation(originalMethod),method_getTypeEncoding(originalMethod));
	
1
* **更换class的imp**:
	method_exchangeImplementations(originalMethod, swizzledMethod);
	

###insightTookit,Debug模式剖析App

  1. app 界面层级信息查看 viewer

  2. app 数据层信息索引查看

    思路同上。。。。O(∩_∩)O~ (关键是: 数据层dic arr 需要规划清晰才好dump 出来)

  3. innerConsole 内嵌控制台

    innerCS

  4. 全局httpVerb swizzing截断获取

		+ (void)injectWillSendRequestIntoDelegateClass:(Class)cls;
	{
	    SEL selector = @selector(connection:willSendRequest:redirectResponse:);
	    SEL swizzledSelector = [self swizzledSelectorForSelector:selector];
	    
	    Protocol *protocol = @protocol(NSURLConnectionDataDelegate);
	    if (!protocol) {
	        protocol = @protocol(NSURLConnectionDelegate);
	    }
	    
	    struct objc_method_description methodDescription = protocol_getMethodDescription(protocol, selector, NO, YES);
	    
	    typedef NSURLRequest *(^NSURLConnectionWillSendRequestBlock)(id <NSURLConnectionDelegate> slf, NSURLConnection *connection, NSURLRequest *request, NSURLResponse *response);
	    
	    NSURLConnectionWillSendRequestBlock undefinedBlock = ^NSURLRequest *(id <NSURLConnectionDelegate> slf, NSURLConnection *connection, NSURLRequest *request, NSURLResponse *response) {
	        [self domainControllerSwizzleGuardForSwizzledObject:slf selector:selector implementationBlock:^{
	            [[swizzOnNSURLConnection defaultInstance] connection:connection willSendRequest:request redirectResponse:response];
	        }];
	        
	        return request;
	    };
	    
	    NSURLConnectionWillSendRequestBlock implementationBlock = ^NSURLRequest *(id <NSURLConnectionDelegate> slf, NSURLConnection *connection, NSURLRequest *request, NSURLResponse *response) {
	        NSURLRequest *returnValue = ((id(*)(id, SEL, id, id, id))objc_msgSend)(slf, swizzledSelector, connection, request, response);
	        undefinedBlock(slf, connection, request, response);
	        return returnValue;
	    };
	    
	    [self replaceImplementationOfSelector:selector withSelector:swizzledSelector forClass:cls withMethodDescription:methodDescription implementationBlock:implementationBlock undefinedBlock:undefinedBlock];
	}
	
  1. 堆栈中的object graph in heap 绝对搜索定位

    [Beagle is an Objective C debugging tool that can sniff out class instances on the heap.] https://github.com/heardrwt/RHObjectiveBeagle

    [Simple NSObject-category wrapper for <objc/runtime.h>] https://github.com/garnett/DLIntrospection

  2. 程序资源耗费可视化优化

    [Instrumentation for Objective C for debugging and profiling] https://github.com/Cue/hookshot

	$ bin/profile.py
	Using /Users/robbywalker/Library/Application Support/iPhone Simulator/6.0/Applications/C1FB13C1-C230-4435-8A10-F7BED5A6B475/Documents/profile-23274
	message                                                                              calls     ownTime       avgOwn      maxOwn         total
	AppDelegate.doSomethingExpensive                                                     27        562.433ms     20.8309ms   21.1630ms      562.433ms
	UIWebView.webView:decidePolicyForNavigationAction:request:frame:decisionListener:    2         19.480ms      9.7400ms    19.4440ms      20.110ms
	UIWebView._webViewCommonInitWithWebView:scalesPageToFit:shouldEnableReachability:    1         11.725ms      11.7250ms   11.7250ms      12.780ms
	AppDelegate.application:didFinishLaunchingWithOptions:                               1         7.913ms       7.9130ms    7.9130ms       25.179ms
	...
	
  1. 封装好了的方便convincedMethod 4 swizzing

    [Delightful, simple library for aspect oriented programming.] https://github.com/steipete/Aspects

  2. exploring obj-c-runtime ,feeling beauty Hierarchy

    not stop….