vrwim
vrwim

Reputation: 14380

How do I use this private API?

I have found the class PSCellularDataSettingsDetail and its method +(void)setEnabled:(BOOL)enabled;, which I think will give me what I need, which is accessing the mobile data setting.

I found this method by opening up the compiled Preferences.framework using class-dump-z.

Now I found this answer and tried to access the class and method that way, but the class is private too. How can I open this class up to Xcode?

Upvotes: 2

Views: 2536

Answers (2)

Joseph Gagliardo
Joseph Gagliardo

Reputation: 783

If it's a class method like +(void)setEnabled you would just call [MyClass performSelector(@selector(myMethod)] and if it is an instance method you would call it on a variable that is an instance of the class: MyClass *c = [[MyClass alloc] init];

[c performSelector: @selector(myMethod)]

It gets tricky when you need to pass parameters though like in this case, because the only way performSelector can pass parameters is if they are objects not primitives. You can also look into using objc_msgSend.

There is a ton of stuff online explaining how those two work. Either way it's messy to try to call private methods and is very risky.

Upvotes: 4

Joseph Gagliardo
Joseph Gagliardo

Reputation: 783

Have you tried calling performSelector? That is usually the trick to call private methods. Remember all that makes a method private in Objective-C is the fact that it is not advertised in the h file. But if you send a message to an object and the object can respond to that message it will, regardless of what's in the header file.

Upvotes: 5

Related Questions