Why is my app really slow to start when I authoriz

2019-08-17 08:45发布

问题:

I developed a 32-bit application and I want to check if it is compatible with Mojave. According to this thread, I have to authorize my application to control my system in the Accessibility settings in order to use some functionality, which I did.

After I changed this setting, my app now takes around 5 minutes to start. Why? How can I reduce the lauching time without changing the Accessibility settings?


EDIT: I reproduce the problem with a 64-bits application on ElCapitain and Sierra (and I believe it also happens in High Sierra). Here is the procedure to reproduce it.

Create a C++ file like this one:

#include <iostream>
#include <ApplicationServices/ApplicationServices.h>

#define SIZE 1000000000
char dummy[SIZE] = {'a'};

int main()
{
    if(AXIsProcessTrustedWithOptions(NULL))
        std::cout << "Trusted" << std::endl;
    else
        std::cout << "Not trusted" << std::endl;

    return 0;
}

I create a dummy variable in order to increase the size of the binary. I noticed the problem is more notable with large application. Also AXIsProcessTrustedWithOptions returns true if the application is authorized to control the system.

Compile it: clang++ -framework ApplicationServices repro.cpp -o repro

Create a bundle application and put repro in MacOS directory:

repro.app
 |_ Contents
     |_ MacOS
         |_ repro

Execute it for the first time:

$ date && open repro.app && date
Ven 10 mai 2019 16:58:10 CEST
Ven 10 mai 2019 16:58:10 CEST

As we can see, it takes less than a second to execute the application.

Now, add repro.app in the list of trusted applications: System Preferences -> Security & Privacy -> Privacy tab -> Accessibility -> Add repro.app in the list.

Run the app a second time:

$ date && open repro.app && date
Ven 10 mai 2019 17:06:31 CEST
Ven 10 mai 2019 17:06:36 CEST

This time, it takes more than 5 seconds to execute it.

It's relatively fast for this sample binary but the application I work on takes multiple minutes to start. Where is the issue?

回答1:

As explained by @KenThomases in comments, I have to sign my application. To do that, I created a self-signed certificate.

Then, I just have to use codesign to sign my app:

$ codesign --deep -s MyIdentity repro.app

In my toy example, the --deep option is not useful but I have to use it to sign all the dependancies of the application I work on.

To finish, I have to deactivate/reactivate the permissions of my application in the System Preferences. If I run it:

$ date && open repro.app && date
Lun 13 mai 2019 17:27:32 CEST
Lun 13 mai 2019 17:27:32 CEST