How to create a helper application for Mac App to start it on user login?

[亡魂溺海] 提交于 2019-12-03 18:36:03

问题


In fact, I read the following document:

Applications can contain a helper application as a full application bundle, stored inside the main application bundle in the Contents/Library/LoginItems directory. Set either the LSUIElement or LSBackgroundOnly key in the Info.plist file of the helper application’s bundle.

I don't quite understand it, anyone knows how to do?

and also, what does this mean:

Note: Before calling the SMLoginItemSetEnabled function, first register with Launch Services by calling the LSRegisterURL function with the URL for the helper application bundle.

is there any example about how to use LSRegisterURL and SMLoginItemSetEnabled?


回答1:


+ (void)startHelper {
    NSURL *helperURL = [[[NSBundle mainBundle] bundleURL] URLByAppendingPathComponent:@"Contents/Library/LoginItems/YourHelper.app" isDirectory:YES];
    OSStatus status = LSRegisterURL((CFURLRef)helperURL, YES);
    if (status != noErr) {
        NSLog(@"Failed to LSRegisterURL '%@': %jd", helperURL, (intmax_t)status);
    }


    Boolean success = SMLoginItemSetEnabled(CFSTR("com.yourcompany.helper-CFBundleIdentifier-here"), YES);
    if (!success) {
        NSLog(@"Failed to start Helper");
    }
}

Note that the Helper must be packaged with the main app in the "Contents/Library/LoginItems" directory. You will need to create it during the build and copy the helper there.




回答2:


I found a handy link:

http://www.delitestudio.com/2011/10/25/start-dockless-apps-at-login-with-app-sandbox-enabled/

EDIT: sadly this link no longer works. Perhaps someone could suggest a better alternative...

It's a good tutorial for doing registering a Login Item in a Sandboxed environment if that's of use (and we'll all need to eventually!). The important thing is that, annoyingly, you have to copy your built, main app to the Applications folder and don't do what I do which is forget to sandbox the helper-app and add a Application is agent (UIElement) row to the helper's plist with TRUE for the value. NSLog and Console is your old-fashioned debugging friend now.

...just have to figure out how to get the helper app to launch the main app when they're both sandboxed..... Edit: Found this question: Cocoa: Sandbox entitlement to launch another application




回答3:


I've never used LSRegisterURL, I can answer the first question: to set the LSUIElement bit you just simply open the .plist and add the row "Application is agent (UIElement)", then set the value to TRUE.




回答4:


Basically you have to take a look at Launch Services which is just a wrapper around launchd, which you can check out by looking at man launchd in the terminal.

or take a look at this question here at SO How do you make your App open at login?



来源:https://stackoverflow.com/questions/7436318/how-to-create-a-helper-application-for-mac-app-to-start-it-on-user-login

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!