I use standard method swizzling on ARMv7 IOS devices and it works perfect to me.
But when I compile my code for arm64 - it fails to call original method from new method
Main purpose of my swizzling - to use parameter from internal method of my application in another method.
I have original method -(void)insertdata:(id)text
and I want to change it on -(void)patchedCall:(id)text
and call original method in new method.
Code:
static IMP sOriginalImp = NULL;
@interface TextOverrides: NSObject
+(void)load;
-(void)patchedinsert:(id)text;
@end
@implementation TextOverrides
+(void)load
{
//Get Implementation of original method
Class originalClass = NSClassFromString(@"DataViewController");
Method originalMeth = class_getInstanceMethod(originalClass, @selector(insertdata:));
//Save original method implementation
sOriginalImp = method_getImplementation(originalMeth);
// Get implementation of replacement method
Method replacementMeth = class_getInstanceMethod(NSClassFromString(@"TextOverrides"), @selector(patchedCall:));
//Replace methods
method_exchangeImplementations(originalMeth, replacementMeth);
}
-(void)patchedCall:(id)text
{
@synchronized(self){
//Call of original method that we save
sOriginalImp(self, @selector(insertdata:), text);
//Make our code for argument "text"
printf("Here is data%s\n", [text UTF8String]);
}
}
@end
Code fails on calling original method on arm64 architecture:
//Call of original method that we save
sOriginalImp(self, @selector(insertdata:), text);
How can I improve my code to work both on armv7 and arm64?
I dont’t think the loading order of all classes are specified somewhere. So it might be that the
TextOverrides
class is loaded beforeDataViewController
on arm64, but not on armv7. See https://www.mikeash.com/pyblog/friday-qa-2009-05-22-objective-c-class-loading-and-initialization.htmlTo make it stable you should only swizzle methods in a category +load method. Eg: