xcode 如何将沙盒应用程序添加到登录项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11292058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add a sandboxed app to the login items
提问by Tibidabo
I want my app to auto start if the user select the option. The methods I have been using is not allowed anymore in sandboxed apps.
如果用户选择该选项,我希望我的应用程序自动启动。我一直在使用的方法在沙盒应用程序中不再被允许。
I know I have to create a helper to achieve that? Is there a simple tutorial with sample code to active that?
我知道我必须创建一个助手来实现这一目标?是否有一个带有示例代码的简单教程来激活它?
I found this tutorial, but it does not work for me: http://www.delitestudio.com/2011/10/25/start-dockless-apps-at-login-with-app-sandbox-enabled/
我找到了这个教程,但它对我不起作用:http: //www.delitestudio.com/2011/10/25/start-dockless-apps-at-login-with-app-sandbox-enabled/
It is a pretty standard thing to do, I don't understand why there is no example project available.
这是一件非常标准的事情,我不明白为什么没有可用的示例项目。
UPDATE:
更新:
I uploaded a sample project: http://ge.tt/6DntY4K/v/0?c
我上传了一个示例项目:http: //ge.tt/6DntY4K/v/0?c
采纳答案by Tim
You should succeed by using this (disclaimer: my) tutorial, sample project included.
你应该通过使用这个(免责声明:我的)教程成功,包括示例项目。
Update:I've now tested the sample project you've uploaded: It works just fine with me, without any modifications, and launch at login succeeds. The only trick is that the AutoStart.app file has to be placed in the /Applications or ~/Applications folder to be launched successfully at login. This is necessary regardless of whether the app is sandboxed or not. However, there's no official documentation on this, I'm afraid.
更新:我现在已经测试了您上传的示例项目:它对我来说很好用,没有任何修改,并且在登录时启动成功。唯一的技巧是 AutoStart.app 文件必须放在 /Applications 或 ~/Applications 文件夹中才能在登录时成功启动。无论应用程序是否被沙盒化,这都是必要的。但是,恐怕没有关于此的官方文档。
回答by Homer Wang
I've just re-done about 100 trial on Tim's tutorial. Finally I made it work. Although I swear it worked when I first time tried it. In my situation is when I switch "Launch at login" to On, I can only see the helper app launched for just one second right after login. Then it was gone. Manually start the app, I saw the switch was turned off.
我刚刚对Tim 的教程重新做了大约 100 次试验。最后我让它工作了。虽然我发誓它在我第一次尝试时有效。在我的情况下,当我将“登录时启动”切换为“开”时,我只能看到帮助应用程序在登录后仅启动一秒钟。然后就没了。手动启动应用程序,我看到开关被关闭了。
Here was what I found:
这是我发现的:
- my bundle identifier was already in the list of
NSArray *running = [[NSWorkspace sharedWorkspace] runningApplications]
- the status of the
NSRunningApplication *app (bundle name equal to my app)
is:[app isActive] == NO
,[app isHidden] == NO
,[app isTerminated] = NO
- 我的包标识符已经在列表中
NSArray *running = [[NSWorkspace sharedWorkspace] runningApplications]
- 的状态
NSRunningApplication *app (bundle name equal to my app)
是:[app isActive] == NO
,[app isHidden] == NO
,[app isTerminated] = NO
So I made some modification to the code like:
所以我对代码做了一些修改,比如:
BOOL alreadyRunning = NO;
BOOL isActive = NO; // my modification
NSArray *running = [[NSWorkspace sharedWorkspace] runningApplications];
for (NSRunningApplication *app in running) {
if ([[app bundleIdentifier] isEqualToString:@"com.mybundleidentifier"]) {
alreadyRunning = YES;
isActive = [app isActive]; // my modification
}
}
if (!alreadyRunning || !isActive) { // my modification
....
回答by Remear
You could try using the Service Management Framework
你可以尝试使用 Service Management Framework
As referenced from http://developer.apple.com/library/mac/#documentation/Security/Conceptual/AppSandboxDesignGuide/DesigningYourSandbox/DesigningYourSandbox.html...
To create a login item for your sandboxed app, use the SMLoginItemSetEnabled
function (declared in ServiceManagement/SMLoginItem.h
) as described in Adding Login Items Using the Service Management Framework
in Daemons and Services Programming Guide
.
要为沙盒应用程序创建登录项,请使用Daemons 和 中所述的SMLoginItemSetEnabled
函数(在 中声明ServiceManagement/SMLoginItem.h
)。Adding Login Items Using the Service Management Framework
Services Programming Guide
(With App Sandbox, you cannot create a login item using functions in the LSSharedFileList.h
header file. For example, you cannot use the function LSSharedFileListInsertItemURL
. Nor can you manipulate the state of launch services, such as by using the function LSRegisterURL
).
(使用App Sandbox,您不能使用LSSharedFileList.h
头文件中的函数创建登录项。例如,您不能使用函数LSSharedFileListInsertItemURL
。也不能操纵启动服务的状态,例如使用函数LSRegisterURL
)。