最近统计了一下Runtime的常用方法,有一些在我们的工程中能起到很巧妙的作用,给大家共享下。
Runtime中所有方法的解释可以看这篇文章;http://www.jianshu.com/p/a31c15064a98
1.实现app的热更新
通过Runtime强大的查找、替换、添加方法和类的功能,可以实现app的热更新,直接更换app的原有代码来实现新的功能和解决bug。 常用的有JSPath和Wax框架。具体的使用可以看这里:http://www.cocoachina.com/ios/20150709/12468.html
2.巧妙的实现方法监控,比如一个方法调用了几次等,可以通过category来实现,而不用修改原类添加太多的冗余代码。通过一个Swizzling类和一个Category类就可以实现了。代码如下:
Swizzling类:主要是是替换Class里面的实例方法和类方法
#import <Foundation/Foundation.h>
@interface SwizzlingHook : NSObject
+ (void)swizzlingInstanceMethodInClass:(Class)cls originalSelector:(SEL)originalSelector swizzledSelector:(SEL)swizzledSelector;
+ (void)swizzlingClassMethodInClass:(Class)cls originalSelector:(SEL)originalSelector swizzledSelector:(SEL)swizzledSelector;
@end
#import "SwizzlingHook.h"
#import <objc/runtime.h>
@implementation SwizzlingHook
+ (void)swizzlingInstanceMethodInClass:(Class)cls originalSelector:(SEL)originalSelector swizzledSelector:(SEL)swizzledSelector {
Class class = cls;
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
BOOL didAdd = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
if (didAdd) {
class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
}else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}
+ (void)swizzlingClassMethodInClass:(Class)cls originalSelector:(SEL)originalSelector swizzledSelector:(SEL)swizzledSelector {
Class class = cls;
Method originalMethod = class_getClassMethod(class, originalSelector);
Method swizzledMethod = class_getClassMethod(class, swizzledSelector);
BOOL didAdd = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
if (didAdd) {
class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
}else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}
Category类:我们用UIViewController来实现下:
#import <UIKit/UIKit.h>
@interface UIViewController (Hook)
+ (void)load;
@end
#import "UIViewController+Hook.h"
#import "SwizzlingHook.h"
#import <objc/runtime.h>
@implementation UIViewController (Hook)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[SwizzlingHook swizzlingInstanceMethodInClass:self originalSelector:@selector(viewDidAppear:) swizzledSelector:@selector(my_viewDidAppear:)];
});
}
-(void)my_viewDidAppear:(BOOL)animated {
NSLog(@"send message: %@ did appear",self);
[self my_viewDidAppear:animated];
}
@end
3.为Category添加实例变量。
@interface UIViewController (Hook)
@property (nonatomic, assign) NSInteger count;
@end
#import "UIViewController+Hook.h"
#import "SwizzlingHook.h"
#import <objc/runtime.h>
@implementation UIViewController (Hook)
@dynamic count;
static void *k_countKey = &k_countKey;
- (NSInteger)count {
NSNumber *coutNumber = objc_getAssociatedObject(self, k_countKey);
if (coutNumber) {
return [coutNumber integerValue];
}else {
return 0;
}
}
- (void)setCount:(NSInteger)newCount {
objc_setAssociatedObject(self, k_countKey, [NSNumber numberWithInteger:newCount], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end
4.可以通过category添加protocal
- (void) addProtocol {
class_addProtocol([self class], @protocol(CustomProtocol));
}