我需要在系统启动时启动我的应用程序,但是问题是:它已经在App Store中,因此我必须遵循一些规则,例如使用沙箱。这导致所需功能失败,例如LSSharedFileListInsertItemURL和SMLoginItemSetEnabled。在这种情况下,我该怎么办?
最佳答案
最近,我经历了相同的过程,不幸的是,使用沙箱比以前做起来要容易得多。我制作了一个测试应用程序,其中包含非常详细的说明,现在为on Github
笔记
此演示应用程序和您的应用程序仅在中部署了,最好在/Applications/MyGreat.app
中部署,并且将无法从Xcode调试文件夹可靠地运行。
项目设定
这些是我的项目的设置,与该实现完美配合。
Contents/Library/LoginItems
离开Copy。将您的帮助程序从左侧的“产品”拖到表格 View 中。 主要应用程式程式码设定
#import <ServiceManagement/ServiceManagement.h>
包括在您的.h文件中StartAtLoginController.h
导入到您的h文件中。 - (IBAction)checkChanged:(id)sender
之类的方法,我将一个简单的复选框与StandardUserDefaults绑定(bind)在一起。 (如果选择执行其他操作,则此实现可能会有所不同。)我还将复选框绑定(bind)到IBOutlet NSButton *loginCheck;
以确定其状态。这也可以通过[[NSUserDefaults standardUserDefaults] boolForKey:YourKey]
StartAtLoginController *loginController = [[StartAtLoginController alloc] init];
[loginController setBundle:[NSBundle bundleWithPath:[[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"Contents/Library/LoginItems/HelperApp.app"]]];
// Change "HelperApp.app" to the name of your helper
if ([loginCheck state]) {
if (![loginController startAtLogin]) {
[loginController setStartAtLogin: YES];
}
} else {
if ([loginController startAtLogin]) {
[loginController setStartAtLogin:NO];
}
}
if ([loginController startAtLogin]) {
NSLog(@"Error");
}
在启用或禁用设置以确保其正常工作后进行检查。或这个:
BOOL startsAtLogin = [loginController startAtLogin];
if (startsAtLogin) {
// Do stuff
}
如果启用了登录帮助器,请执行某些操作。
助手应用程序代码设置
确保在您的实现中大力测试此代码。
Application is agent (UIElement)
和YES
作为值(这将抑制应用程序每次用户启用登录时都会启动停靠图标)我还删除了所有内容,除了界面构建器- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
并将其替换为- (void)applicationWillFinishLaunching:(NSNotification *)aNotification
NSString *appPath = [[[[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByDeletingLastPathComponent] stringByDeletingLastPathComponent] stringByDeletingLastPathComponent];
// This string takes you from MyGreat.App/Contents/Library/LoginItems/MyHelper.app to MyGreat.App This is an obnoxious but dynamic way to do this since that specific Subpath is required
NSString *binaryPath = [[NSBundle bundleWithPath:appPath] executablePath]; // This gets the binary executable within your main application
[[NSWorkspace sharedWorkspace] launchApplication:binaryPath];
[NSApp terminate:nil];
此代码找到您的主应用程序,确定它是二进制可执行文件(需要在沙箱中启动该应用程序)打开您的应用程序,然后退出
部署
为自己或Mac App Store部署应用程序时,您应该做的最后一件事是从“已存档”项中删除“助手”应用程序。为此,请导航至HelperApp的目标->build设置->跳过安装,然后为发布设置为是。 Apple在(http://developer.apple.com/library/ios/#documentation/ToolsLanguages/Conceptual/Xcode4UserGuide/000-About_Xcode/about.html)提供了更多信息。
关于objective-c - Mac OS X : start on launch while in app store?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11536477/