(iOS, TheOS) %hook into global app function

Aleksander Azizi picture Aleksander Azizi · Dec 25, 2013 · Viewed 8k times · Source

I am looking for a global function for apps in iOS 7.

More specifically, I want to injected code into the app(s) upon launch, which will only effect the app, and not the SpringBoard.

I have tried a couple of things but they only affect the SpringBoard:

%hook SBApplicationIcon
    - (id)application {
        return %orig;
    }
    - (id)initWithApplication:(id)arg1 {
        return %orig;
    }
%end

%hook SBApplicationController
    - (id)init {
         return %orig;
    }
%end

%hook SBUIController
    - (void)launchIcon:(id)arg1 fromLocation:(int)arg2 {
        %orig;
    }
    - (id)contentView {
        return %orig;
    }
%end

%hook SBApplication
    - (void)didLaunch:(id)arg1 {
        %orig;
    }
%end

These are just a couple of examples of functions I've tried.

I suspect the filter needs to be changed as well, but that depends on where the function is located ofc (com.apple.springboard is set atm).

I was received a tip to set the filter to *, but that doesn't do me much good if I don't know what function to %hook.

Please explain your answer if possible.

Answer

Nate picture Nate · Dec 28, 2013

Your code is only running in SpringBoard because you've chosen to hook methods in SpringBoard classes (e.g. SBUIController, SBApplicationController, etc.), and your filter is set to only hook SpringBoard itself.

Try checking out the MobileSubstrate docs here

I'm not 100% sure I understand what you're trying to do, but it sounds like you simply want any method that will run in all normal "apps"?

If so, you might change your filter to hook everything that uses UIKit:

Filter = {
  Bundles = (com.apple.UIKit);
};

You could then try using MSHookFunction() to hook a C-function, as shown in this example.

In your code, try hooking UIApplicationMain(), which I believe all normal apps would use.

Update:

Another potential technique would be to hook any of the usual launch callback methods from the UIApplicationDelegate protocol. In order to using hooking, though, you need to discover which classes implement this protocol. See this answer for an example of doing this (with another protocol).