Is there a way to access a method with 4 parameters which is not declared in header file?
1. Will Apple reject if
is used?NSInvocation
NSInvocation
is a public API, and using it is not grounds for rejection in and of itself. However, using it to call private methods in Apple frameworks is definitely grounds for rejection. And even if you’re not shipping via the App Store, using private methods can lead to binary compatibility problems in the future. Don’t do this.
WARNING Do not do any of this for Apple classes in product code.
2. I tried Managing Functions and Function Pointers but I couldn't use as one of the parameter is a block for call back.
It’s hard to say what’s going on here without more context. Pasted in below is a full program that implements my take on your code:
@import Foundation;
@import ObjectiveC;
@interface Main : NSObject
@end
@implementation Main
typedef void (^Handler)(NSDictionary *info, NSError *error);
- (void)run {
typedef void (*MethodType)(id, SEL, NSString *, NSString *, Handler);
MethodType methodToCall = (MethodType) objc_msgSend;
methodToCall(
self,
@selector(testWithString1:string2:handler:),
@"s1",
@"s2",
^(NSDictionary *info, NSError *error) {
NSLog(@"handler called, info: %@, error: %@", info, error);
}
);
}
- (void)testWithString1:(NSString *)string1 string2:(NSString *)string2 handler:(Handler)handler {
handler(@{ @"a": string1, @"b": string2 }, nil);
}
@end
int main(int argc, char **argv) {
#pragma unused(argc)
#pragma unused(argv)
@autoreleasepool {
[[[Main alloc] init] run];
}
return EXIT_SUCCESS;
}
Running it here (macOS 10.14.6, Xcode 10.3) it prints:
2019-08-23 09:48:30.561572+0100 xxot[86465:7744413] handler called, info: {
a = s1;
b = s2;
}, error: (null)
3. I tried category as well. It didn't work as the category method is not declared in header file.
I’m not sure what you mean by this. The purpose of the category is to declare methods, so if it doesn’t declare a method then you can solve that by adding a method to the category.
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"