Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kikeenrique/088437ec83211a48e7f6c9f08dbb7ff6 to your computer and use it in GitHub Desktop.
Save kikeenrique/088437ec83211a48e7f6c9f08dbb7ff6 to your computer and use it in GitHub Desktop.
calling private iOS apis using nsinvocation
// https://www.swiftjectivec.com/calling-private-ios-apis-using-nsinvocation/
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
// Load in the private framework
[[NSBundle bundleWithPath:@"/System/Library/PrivateFrameworks/TipKit.framework"] load];
// Get our class type
Class TPKContentView = NSClassFromString(@"TPKContentView");
// Ignore undefined selector warnings below
// Because clang can't tell me who I am
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
// Create our two selectors to call
SEL siriIconSel = @selector(SiriIconWithTraitCollection:);
SEL tipsIconSel = @selector(TipsIconWithTraitCollection:);
#pragma clang diagnostic pop
// These will be the result of the method invocations
id returnValSiri;
id returnValTips;
// I know these methods take in a trait collection
// From trial and error so I'll set it aside here
// For readability
UITraitCollection *traits = self.traitCollection;
// Create the invocations. The first one, for Siri,
// Is commented step by step...
// Create an invocation using the type's method
// Signature we want. So, the TPContentView we
// Made earlier, and the selector we created above.
NSInvocation *siriIconInvocation = [NSInvocation invocationWithMethodSignature:[TPKContentView methodSignatureForSelector:siriIconSel]];
// Set that selector...
[siriIconInvocation setSelector:siriIconSel];
// And the target that'll attempt to respond to it...
[siriIconInvocation setTarget:TPKContentView];
// Pass the method the trait collection as
// An argument, see why it's at index 2
// After the code sample below.
[siriIconInvocation setArgument:&traits atIndex:2];
// Perform it, watch magic happen or
// Your app explode...
[siriIconInvocation invoke];
// And assign the result to a variable.
[siriIconInvocation getReturnValue:&returnValSiri];
// And another for the tips
NSInvocation *tipsIconInvocation = [NSInvocation invocationWithMethodSignature:[TPKContentView methodSignatureForSelector:tipsIconSel]];
[tipsIconInvocation setSelector:tipsIconSel];
[tipsIconInvocation setTarget:TPKContentView];
[tipsIconInvocation setArgument:&traits atIndex:2];
[tipsIconInvocation invoke];
[tipsIconInvocation getReturnValue:&returnValTips];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment