pAK*_*-76 8 macos cocoa objective-c
我需要在系统启动时启动我的应用程序,但问题是:它已经在App Store中,所以我必须遵循一些规则,比如使用沙盒.这导致所需函数失败,如LSSharedFileListInsertItemURL和SMLoginItemSetEnabled.在这种情况下我该怎么做?
Kei*_*ley 16
我最近经历了同样的过程,不幸的是,使用沙盒它并不像以前那么容易.我使用非常详细的说明制作了一个测试应用程序,现在在Github上
这个演示应用程序和应用程序,如果他们被部署只会工作的最好/Applications/MyGreat.app,并不会从Xcode的debug文件夹中可靠地工作.
这些是我的项目的设置,与此实现完美配合.
Contents/Library/LoginItems仅在未选中安装时使子路径保留复制.将助手应用程序从左侧的Products拖到tableview中.#import <ServiceManagement/ServiceManagement.h>在.h文件中StartAtLoginController.h到您的h文件中.- (IBAction)checkChanged:(id)sender我将一个简单的复选框绑定到StandardUserDefaults.(如果您选择执行其他操作,则此实现可能会有所不同.)我还将复选框绑定到IBOutlet NSButton *loginCheck;以确定其状态.这也可以通过[[NSUserDefaults standardUserDefaults] boolForKey:YourKey]在.m文件中实现与此类似的代码.
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];
}
}
Run Code Online (Sandbox Code Playgroud)而已.正如您在此项目中看到的,您可能希望使用其他一些方法,例如:
if ([loginController startAtLogin]) {
NSLog(@"Error");
}
Run Code Online (Sandbox Code Playgroud)
在启用或禁用设置以确保其正常工作后进行检查.或这个:
BOOL startsAtLogin = [loginController startAtLogin];
if (startsAtLogin) {
// Do stuff
}
Run Code Online (Sandbox Code Playgroud)
如果启用了登录助手,则执行某些操作.
确保使用您的实现大力测试此代码.
Application is agent (UIElement)和YESValue (这将阻止应用程序在每次用户启用登录时闪存停靠图标)我还删除了除界面构建器中的App Delegate之外的所有内容- (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];
Run Code Online (Sandbox Code Playgroud)
此代码找到您的主应用程序,确定它的二进制可执行文件(在沙箱中启动应用程序所需)打开您的应用程序,然后退出
在为自己或Mac App Store部署应用程序时,您应该做的最后一件事是从已存档项目中删除您的Helper应用程序.通过导航到HelperApp的目标 - >构建设置 - >跳过安装并为发布设置是,执行此操作.Apple提供了更多信息(http://developer.apple.com/library/ios/#documentation/ToolsLanguages/Conceptual/Xcode4UserGuide/000-About_Xcode/about.html)
| 归档时间: |
|
| 查看次数: |
3204 次 |
| 最近记录: |