iOS编程——Runtime在工程中的常用方法

最近统计了一下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));
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值