Skip to content

Instantly share code, notes, and snippets.

@aydenp
Last active December 23, 2019 04:39
Show Gist options
  • Save aydenp/5c3da54a8d24efc0a3cb63529f027f90 to your computer and use it in GitHub Desktop.
Save aydenp/5c3da54a8d24efc0a3cb63529f027f90 to your computer and use it in GitHub Desktop.
leave me alone!
//
// NSAssertionHandler+Goodbye.m
// please only ever use this for patching out apple checks, never in prod
//
// Created by Ayden Panhuyzen on 2019-12-20.
// Copyright © 2019 Ayden Panhuyzen. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
@implementation NSAssertionHandler (BeGone)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [self class];
SEL originalSelector = @selector(handleFailureInMethod:object:file:lineNumber:description:);
SEL swizzledSelector = @selector(xxx_handleFailureInMethod:object:file:lineNumber:description:);
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
BOOL didAddMethod =
class_addMethod(class,
originalSelector,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod));
if (didAddMethod) {
class_replaceMethod(class,
swizzledSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
});
}
- (void)xxx_handleFailureInMethod:(SEL)selector object:(id)object file:(NSString *)fileName lineNumber:(NSInteger)line description:(NSString *)format, ... {
va_list args;
va_start(args, format);
NSString *desc = [[NSString alloc] initWithFormat:format arguments:args];
va_end(args);
NSLog(@"[NSAssertionHandlerBeGone] we almost had an exception calling +[%@ %@] but instead we did not lol: %@", object, NSStringFromSelector(selector), desc);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment