Skip to content

Instantly share code, notes, and snippets.

@aaronksaunders
Created June 12, 2013 14:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronksaunders/dee39ad018c20f5b592b to your computer and use it in GitHub Desktop.
Save aaronksaunders/dee39ad018c20f5b592b to your computer and use it in GitHub Desktop.
Working with TiExceptionHandler to better capture issues within my Appcelerator IOS Applications
// swap out the TiExceptionHandler classes for my own classes.
// will need to call the original method functionality at some point
-(void) resetErrorHandling
{
[ComCiErrorHandlerModule swapMethod:@selector(reportScriptError:)
withMethod:@selector(reportScriptError:)
origClass:[TiExceptionHandler class]
newClass:[ComCiErrorHandlerModule class]];
[ComCiErrorHandlerModule swapMethod:@selector(showScriptError:)
withMethod:@selector(showScriptError:)
origClass:[TiExceptionHandler class]
newClass:[ComCiErrorHandlerModule class]];
[ComCiErrorHandlerModule swapMethod:@selector(reportException:withStackTrace:)
withMethod:@selector(reportScriptError:scriptError:)
origClass:[TiExceptionHandler class]
newClass:[ComCiErrorHandlerModule class]];
}
// these are my replacement methods
- (void)showScriptError:(TiScriptError *)error
{
NSLog(@"initializing NEW showScriptError: %@ ", [error description]);
}
- (void)reportScriptError:(TiScriptError *)scriptError
{
NSLog(@"reportScriptError: %@ ", [scriptError description]);
}
- (void)reportException:(NSException *)exception withStackTrace:(NSArray *)stackTrace
{
NSLog(@"initializing NEW reportException");
}
// Here is the magic of Swizzling to get it done
+ (void)swapMethod:(SEL)origM withMethod:(SEL)newM origClass:(Class)origClass newClass:(Class)newClass {
Method origMethod = class_getInstanceMethod(origClass, origM);
Method newMethod = class_getInstanceMethod(newClass, newM);
if (class_addMethod(origClass, origM, method_getImplementation(newMethod), method_getTypeEncoding(newMethod))) {
class_replaceMethod(origClass, newM, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
} else {
method_exchangeImplementations(origMethod, newMethod);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment