消息转发防止Crash

@implementation NSObject (Crash)

+ (void)load {
    [self exchangeInstanceMethodWithOriginalSelector:@selector(methodSignatureForSelector:) swizzledSelector:@selector(crash_methodSignatureForSelector:)];
    [self exchangeInstanceMethodWithOriginalSelector:@selector(forwardInvocation:) swizzledSelector:@selector(crash_forwardInvocation:)];
}

+ (void)exchangeInstanceMethodWithOriginalSelector:(SEL)originalSelector swizzledSelector:(SEL)swizzledSelector {
    Method originalMethod = class_getInstanceMethod(self, originalSelector);
    Method swizzledMethod = class_getInstanceMethod(self, swizzledSelector);
    BOOL didAddMethod = class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
    if (didAddMethod) {
        class_replaceMethod(self, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
    } else {
        method_exchangeImplementations(originalMethod, swizzledMethod);
    }
}

- (NSMethodSignature *)crash_methodSignatureForSelector:(SEL)aSelector {
    NSMethodSignature *methodSignature = [self crash_methodSignatureForSelector:aSelector];

    NSLog(@"%s %@ %@ %@", __func__, NSStringFromClass([self class]), methodSignature, NSStringFromSelector(aSelector));
    for (NSUInteger i = 0; i < methodSignature.numberOfArguments; ++i) {
        NSLog(@"argument type %s", [methodSignature getArgumentTypeAtIndex:i]);
    }
    NSLog(@"return type %s", methodSignature.methodReturnType);

    if ([NSStringFromClass([self class]) hasPrefix:@"xxx"] && methodSignature == nil) {
        methodSignature = [self methodSignatureForSelector:@selector(crash_invokeWithInvocation:)];
    }

    return methodSignature;
}

- (void)crash_forwardInvocation:(NSInvocation *)anInvocation {
    NSLog(@"%s %@ %@", __func__, NSStringFromClass([self class]), anInvocation);
    NSLog(@"%@", anInvocation.target);
    NSLog(@"%@", NSStringFromSelector(anInvocation.selector));
    NSLog(@"%@", anInvocation.methodSignature);
    for (NSUInteger i = 0; i < anInvocation.methodSignature.numberOfArguments; ++i) {
        NSLog(@"argument type %s", [anInvocation.methodSignature getArgumentTypeAtIndex:i]);
    }
    NSLog(@"return type %s", anInvocation.methodSignature.methodReturnType);

    NSInvocation *forwardInvocation = [NSInvocation invocationWithMethodSignature:anInvocation.methodSignature];
    forwardInvocation.target = self;
    forwardInvocation.selector = @selector(crash_invokeWithInvocation:);
    [forwardInvocation setArgument:&anInvocation atIndex:2];

    [forwardInvocation invoke];
}

- (void)crash_invokeWithInvocation:(NSInvocation *)anInvocation {
    printf("########################\n");
    printf("----- 警告:缺少方法 -----\n");
    printf("方法:%s\n", NSStringFromSelector(anInvocation.selector).UTF8String);
    printf("类:%s\n", NSStringFromClass([anInvocation.target class]).UTF8String);
    printf("########################\n");
}

@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值